Miroslav Lichvar 0f2592
commit f32a8469a236728fb158ce997385b53f92b821cc
Miroslav Lichvar 0f2592
Author: Jacob Keller <jacob.e.keller@intel.com>
Miroslav Lichvar 0f2592
Date:   Tue Nov 23 14:43:26 2021 -0800
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
    phc2sys: move read_phc into clock_adj.c
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    The read_phc function implemented in phc2sys.c is used to perform clock
Miroslav Lichvar 0f2592
    comparison between two arbitrary clocks using clock_gettime.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    This support is used to allow phc2sys to work on any pair of clocks and
Miroslav Lichvar 0f2592
    is implemented in a very similar manner as the kernel PTP_SYS_OFFSET
Miroslav Lichvar 0f2592
    ioctls.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Make this function easier to re-use by moving it out of phc2sys.c and
Miroslav Lichvar 0f2592
    into a more accessible location. clockadj.c seems like a reasonable
Miroslav Lichvar 0f2592
    location as this file has many functions which deal with clockid_t
Miroslav Lichvar 0f2592
    values, and this functionality is tangentially related to adjusting
Miroslav Lichvar 0f2592
    clocks.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Moving this function will allow using it in the phc_ctl program in a
Miroslav Lichvar 0f2592
    future change.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Notice that read_phc returned 0 on failure and 1 on success. This is
Miroslav Lichvar 0f2592
    fairly non-standard, so lets update clockadj_compare to return 0 on
Miroslav Lichvar 0f2592
    success and -1 on failure. Fix the call sites to check correctly and
Miroslav Lichvar 0f2592
    report an error.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
diff --git a/clockadj.c b/clockadj.c
Miroslav Lichvar 0f2592
index b5c78cd..e8c5789 100644
Miroslav Lichvar 0f2592
--- a/clockadj.c
Miroslav Lichvar 0f2592
+++ b/clockadj.c
Miroslav Lichvar 0f2592
@@ -139,6 +139,37 @@ int clockadj_max_freq(clockid_t clkid)
Miroslav Lichvar 0f2592
 	return f;
Miroslav Lichvar 0f2592
 }
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
+int clockadj_compare(clockid_t clkid, clockid_t sysclk, int readings,
Miroslav Lichvar 0f2592
+		     int64_t *offset, uint64_t *ts, int64_t *delay)
Miroslav Lichvar 0f2592
+{
Miroslav Lichvar 0f2592
+	struct timespec tdst1, tdst2, tsrc;
Miroslav Lichvar 0f2592
+	int i;
Miroslav Lichvar 0f2592
+	int64_t interval, best_interval = INT64_MAX;
Miroslav Lichvar 0f2592
+
Miroslav Lichvar 0f2592
+	/* Pick the quickest clkid reading. */
Miroslav Lichvar 0f2592
+	for (i = 0; i < readings; i++) {
Miroslav Lichvar 0f2592
+		if (clock_gettime(sysclk, &tdst1) ||
Miroslav Lichvar 0f2592
+				clock_gettime(clkid, &tsrc) ||
Miroslav Lichvar 0f2592
+				clock_gettime(sysclk, &tdst2)) {
Miroslav Lichvar 0f2592
+			pr_err("failed to read clock: %m");
Miroslav Lichvar 0f2592
+			return -1;
Miroslav Lichvar 0f2592
+		}
Miroslav Lichvar 0f2592
+
Miroslav Lichvar 0f2592
+		interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
Miroslav Lichvar 0f2592
+			tdst2.tv_nsec - tdst1.tv_nsec;
Miroslav Lichvar 0f2592
+
Miroslav Lichvar 0f2592
+		if (best_interval > interval) {
Miroslav Lichvar 0f2592
+			best_interval = interval;
Miroslav Lichvar 0f2592
+			*offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC +
Miroslav Lichvar 0f2592
+				tdst1.tv_nsec - tsrc.tv_nsec + interval / 2;
Miroslav Lichvar 0f2592
+			*ts = tdst2.tv_sec * NS_PER_SEC + tdst2.tv_nsec;
Miroslav Lichvar 0f2592
+		}
Miroslav Lichvar 0f2592
+	}
Miroslav Lichvar 0f2592
+	*delay = best_interval;
Miroslav Lichvar 0f2592
+
Miroslav Lichvar 0f2592
+	return 0;
Miroslav Lichvar 0f2592
+}
Miroslav Lichvar 0f2592
+
Miroslav Lichvar 0f2592
 void sysclk_set_leap(int leap)
Miroslav Lichvar 0f2592
 {
Miroslav Lichvar 0f2592
 	clockid_t clkid = CLOCK_REALTIME;
Miroslav Lichvar 0f2592
diff --git a/clockadj.h b/clockadj.h
Miroslav Lichvar 0f2592
index 43325c8..b63ae38 100644
Miroslav Lichvar 0f2592
--- a/clockadj.h
Miroslav Lichvar 0f2592
+++ b/clockadj.h
Miroslav Lichvar 0f2592
@@ -63,6 +63,24 @@ void clockadj_step(clockid_t clkid, int64_t step);
Miroslav Lichvar 0f2592
  */
Miroslav Lichvar 0f2592
 int clockadj_max_freq(clockid_t clkid);
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
+/**
Miroslav Lichvar 0f2592
+ * Compare offset between two clocks
Miroslav Lichvar 0f2592
+ * @param clkid     A clock ID obtained using phc_open() or CLOCK_REALTIME
Miroslav Lichvar 0f2592
+ * @param sysclk    A clock ID obtained using phc_open() or CLOCK_REALTIME
Miroslav Lichvar 0f2592
+ * @param readings  Number of readings to try
Miroslav Lichvar 0f2592
+ * @param offset    On return, the nanoseconds offset between the clocks
Miroslav Lichvar 0f2592
+ * @param ts        On return, the time of sysclk in nanoseconds that was used
Miroslav Lichvar 0f2592
+ * @param delay     On return, the interval between two reads of sysclk
Miroslav Lichvar 0f2592
+ * @return Zero on success and non-zero on failure.
Miroslav Lichvar 0f2592
+ *
Miroslav Lichvar 0f2592
+ * Compare the offset between two clocks in a similar manner as the
Miroslav Lichvar 0f2592
+ * PTP_SYS_OFFSET ioctls. Performs multiple reads of sysclk with a read of
Miroslav Lichvar 0f2592
+ * clkid between in order to calculate the time difference of sysclk minus
Miroslav Lichvar 0f2592
+ * clkid.
Miroslav Lichvar 0f2592
+ */
Miroslav Lichvar 0f2592
+int clockadj_compare(clockid_t clkid, clockid_t sysclk, int readings,
Miroslav Lichvar 0f2592
+		     int64_t *offset, uint64_t *ts, int64_t *delay);
Miroslav Lichvar 0f2592
+
Miroslav Lichvar 0f2592
 /**
Miroslav Lichvar 0f2592
  * Set the system clock to insert/delete leap second at midnight.
Miroslav Lichvar 0f2592
  * @param leap  +1 to insert leap second, -1 to delete leap second,
Miroslav Lichvar 0f2592
diff --git a/phc2sys.c b/phc2sys.c
Miroslav Lichvar 0f2592
index a36cbe0..7a547fa 100644
Miroslav Lichvar 0f2592
--- a/phc2sys.c
Miroslav Lichvar 0f2592
+++ b/phc2sys.c
Miroslav Lichvar 0f2592
@@ -486,37 +486,6 @@ static void reconfigure(struct phc2sys_private *priv)
Miroslav Lichvar 0f2592
 	pr_info("selecting %s as the master clock", src->device);
Miroslav Lichvar 0f2592
 }
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
-static int read_phc(clockid_t clkid, clockid_t sysclk, int readings,
Miroslav Lichvar 0f2592
-		    int64_t *offset, uint64_t *ts, int64_t *delay)
Miroslav Lichvar 0f2592
-{
Miroslav Lichvar 0f2592
-	struct timespec tdst1, tdst2, tsrc;
Miroslav Lichvar 0f2592
-	int i;
Miroslav Lichvar 0f2592
-	int64_t interval, best_interval = INT64_MAX;
Miroslav Lichvar 0f2592
-
Miroslav Lichvar 0f2592
-	/* Pick the quickest clkid reading. */
Miroslav Lichvar 0f2592
-	for (i = 0; i < readings; i++) {
Miroslav Lichvar 0f2592
-		if (clock_gettime(sysclk, &tdst1) ||
Miroslav Lichvar 0f2592
-				clock_gettime(clkid, &tsrc) ||
Miroslav Lichvar 0f2592
-				clock_gettime(sysclk, &tdst2)) {
Miroslav Lichvar 0f2592
-			pr_err("failed to read clock: %m");
Miroslav Lichvar 0f2592
-			return 0;
Miroslav Lichvar 0f2592
-		}
Miroslav Lichvar 0f2592
-
Miroslav Lichvar 0f2592
-		interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
Miroslav Lichvar 0f2592
-			tdst2.tv_nsec - tdst1.tv_nsec;
Miroslav Lichvar 0f2592
-
Miroslav Lichvar 0f2592
-		if (best_interval > interval) {
Miroslav Lichvar 0f2592
-			best_interval = interval;
Miroslav Lichvar 0f2592
-			*offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC +
Miroslav Lichvar 0f2592
-				tdst1.tv_nsec - tsrc.tv_nsec + interval / 2;
Miroslav Lichvar 0f2592
-			*ts = tdst2.tv_sec * NS_PER_SEC + tdst2.tv_nsec;
Miroslav Lichvar 0f2592
-		}
Miroslav Lichvar 0f2592
-	}
Miroslav Lichvar 0f2592
-	*delay = best_interval;
Miroslav Lichvar 0f2592
-
Miroslav Lichvar 0f2592
-	return 1;
Miroslav Lichvar 0f2592
-}
Miroslav Lichvar 0f2592
-
Miroslav Lichvar 0f2592
 static int64_t get_sync_offset(struct phc2sys_private *priv, struct clock *dst)
Miroslav Lichvar 0f2592
 {
Miroslav Lichvar 0f2592
 	int direction = priv->forced_sync_offset;
Miroslav Lichvar 0f2592
@@ -672,8 +641,10 @@ static int do_pps_loop(struct phc2sys_private *priv, struct clock *clock,
Miroslav Lichvar 0f2592
 		/* If a PHC is available, use it to get the whole number
Miroslav Lichvar 0f2592
 		   of seconds in the offset and PPS for the rest. */
Miroslav Lichvar 0f2592
 		if (src != CLOCK_INVALID) {
Miroslav Lichvar 0f2592
-			if (!read_phc(src, clock->clkid, priv->phc_readings,
Miroslav Lichvar 0f2592
-				      &phc_offset, &phc_ts, &phc_delay))
Miroslav Lichvar 0f2592
+			if (clockadj_compare(src, clock->clkid,
Miroslav Lichvar 0f2592
+					     priv->phc_readings,
Miroslav Lichvar 0f2592
+					     &phc_offset, &phc_ts,
Miroslav Lichvar 0f2592
+					     &phc_delay))
Miroslav Lichvar 0f2592
 				return -1;
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 			/* Convert the time stamp to the PHC time. */
Miroslav Lichvar 0f2592
@@ -781,10 +752,11 @@ static int do_loop(struct phc2sys_private *priv, int subscriptions)
Miroslav Lichvar 0f2592
 				ts += offset;
Miroslav Lichvar 0f2592
 			} else {
Miroslav Lichvar 0f2592
 				/* use phc */
Miroslav Lichvar 0f2592
-				if (!read_phc(priv->master->clkid, clock->clkid,
Miroslav Lichvar 0f2592
-					      priv->phc_readings,
Miroslav Lichvar 0f2592
-					      &offset, &ts, &delay))
Miroslav Lichvar 0f2592
-					continue;
Miroslav Lichvar 0f2592
+				if (clockadj_compare(priv->master->clkid,
Miroslav Lichvar 0f2592
+						     clock->clkid,
Miroslav Lichvar 0f2592
+						     priv->phc_readings,
Miroslav Lichvar 0f2592
+						     &offset, &ts, &delay))
Miroslav Lichvar 0f2592
+					return -1;
Miroslav Lichvar 0f2592
 			}
Miroslav Lichvar 0f2592
 			update_clock(priv, clock, offset, ts, delay);
Miroslav Lichvar 0f2592
 		}
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
commit 96486bda9ac1613fb36feb84d76ababd8972bba6
Miroslav Lichvar 0f2592
Author: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Date:   Tue May 17 12:31:45 2022 +0200
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
    clockadj: Change clockadj_compare() to return errno.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Return -errno from the failed clock_gettime() to allow the callers to
Miroslav Lichvar 0f2592
    check for specific errors.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
diff --git a/clockadj.c b/clockadj.c
Miroslav Lichvar 0f2592
index e8c5789..957dc57 100644
Miroslav Lichvar 0f2592
--- a/clockadj.c
Miroslav Lichvar 0f2592
+++ b/clockadj.c
Miroslav Lichvar 0f2592
@@ -17,6 +17,7 @@
Miroslav Lichvar 0f2592
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Miroslav Lichvar 0f2592
  */
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
+#include <errno.h>
Miroslav Lichvar 0f2592
 #include <math.h>
Miroslav Lichvar 0f2592
 #include <string.h>
Miroslav Lichvar 0f2592
 #include <unistd.h>
Miroslav Lichvar 0f2592
@@ -152,7 +153,7 @@ int clockadj_compare(clockid_t clkid, clockid_t sysclk, int readings,
Miroslav Lichvar 0f2592
 				clock_gettime(clkid, &tsrc) ||
Miroslav Lichvar 0f2592
 				clock_gettime(sysclk, &tdst2)) {
Miroslav Lichvar 0f2592
 			pr_err("failed to read clock: %m");
Miroslav Lichvar 0f2592
-			return -1;
Miroslav Lichvar 0f2592
+			return -errno;
Miroslav Lichvar 0f2592
 		}
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 		interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
Miroslav Lichvar 0f2592
diff --git a/clockadj.h b/clockadj.h
Miroslav Lichvar 0f2592
index b63ae38..6db1d79 100644
Miroslav Lichvar 0f2592
--- a/clockadj.h
Miroslav Lichvar 0f2592
+++ b/clockadj.h
Miroslav Lichvar 0f2592
@@ -71,7 +71,7 @@ int clockadj_max_freq(clockid_t clkid);
Miroslav Lichvar 0f2592
  * @param offset    On return, the nanoseconds offset between the clocks
Miroslav Lichvar 0f2592
  * @param ts        On return, the time of sysclk in nanoseconds that was used
Miroslav Lichvar 0f2592
  * @param delay     On return, the interval between two reads of sysclk
Miroslav Lichvar 0f2592
- * @return Zero on success and non-zero on failure.
Miroslav Lichvar 0f2592
+ * @return Zero on success, or negative error code on failure.
Miroslav Lichvar 0f2592
  *
Miroslav Lichvar 0f2592
  * Compare the offset between two clocks in a similar manner as the
Miroslav Lichvar 0f2592
  * PTP_SYS_OFFSET ioctls. Performs multiple reads of sysclk with a read of
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
commit a523e893a15001025379e3c2dedb231e99cc886f
Miroslav Lichvar 0f2592
Author: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Date:   Thu Mar 24 11:55:35 2022 +0100
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
    sysoff: Change sysoff_measure() to return errno.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Return -errno from failed ioctl instead of the SYSOFF_* enum from the
Miroslav Lichvar 0f2592
    measurement functions to allow the callers to check for specific errors.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
diff --git a/sysoff.c b/sysoff.c
Miroslav Lichvar 0f2592
index 2743859..5d3b907 100644
Miroslav Lichvar 0f2592
--- a/sysoff.c
Miroslav Lichvar 0f2592
+++ b/sysoff.c
Miroslav Lichvar 0f2592
@@ -17,6 +17,7 @@
Miroslav Lichvar 0f2592
  * with this program; if not, write to the Free Software Foundation, Inc.,
Miroslav Lichvar 0f2592
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Miroslav Lichvar 0f2592
  */
Miroslav Lichvar 0f2592
+#include <errno.h>
Miroslav Lichvar 0f2592
 #include <stdio.h>
Miroslav Lichvar 0f2592
 #include <string.h>
Miroslav Lichvar 0f2592
 #include <sys/ioctl.h>
Miroslav Lichvar 0f2592
@@ -38,11 +39,11 @@ static int sysoff_precise(int fd, int64_t *result, uint64_t *ts)
Miroslav Lichvar 0f2592
 	memset(&pso, 0, sizeof(pso));
Miroslav Lichvar 0f2592
 	if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, &pso)) {
Miroslav Lichvar 0f2592
 		pr_debug("ioctl PTP_SYS_OFFSET_PRECISE: %m");
Miroslav Lichvar 0f2592
-		return SYSOFF_RUN_TIME_MISSING;
Miroslav Lichvar 0f2592
+		return -errno;
Miroslav Lichvar 0f2592
 	}
Miroslav Lichvar 0f2592
 	*result = pctns(&pso.sys_realtime) - pctns(&pso.device);
Miroslav Lichvar 0f2592
 	*ts = pctns(&pso.sys_realtime);
Miroslav Lichvar 0f2592
-	return SYSOFF_PRECISE;
Miroslav Lichvar 0f2592
+	return 0;
Miroslav Lichvar 0f2592
 }
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 static int64_t sysoff_estimate(struct ptp_clock_time *pct, int extended,
Miroslav Lichvar 0f2592
@@ -98,10 +99,10 @@ static int sysoff_extended(int fd, int n_samples,
Miroslav Lichvar 0f2592
 	pso.n_samples = n_samples;
Miroslav Lichvar 0f2592
 	if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, &pso)) {
Miroslav Lichvar 0f2592
 		pr_debug("ioctl PTP_SYS_OFFSET_EXTENDED: %m");
Miroslav Lichvar 0f2592
-		return SYSOFF_RUN_TIME_MISSING;
Miroslav Lichvar 0f2592
+		return -errno;
Miroslav Lichvar 0f2592
 	}
Miroslav Lichvar 0f2592
 	*result = sysoff_estimate(&pso.ts[0][0], 1, n_samples, ts, delay);
Miroslav Lichvar 0f2592
-	return SYSOFF_EXTENDED;
Miroslav Lichvar 0f2592
+	return 0;
Miroslav Lichvar 0f2592
 }
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 static int sysoff_basic(int fd, int n_samples,
Miroslav Lichvar 0f2592
@@ -112,10 +113,10 @@ static int sysoff_basic(int fd, int n_samples,
Miroslav Lichvar 0f2592
 	pso.n_samples = n_samples;
Miroslav Lichvar 0f2592
 	if (ioctl(fd, PTP_SYS_OFFSET, &pso)) {
Miroslav Lichvar 0f2592
 		perror("ioctl PTP_SYS_OFFSET");
Miroslav Lichvar 0f2592
-		return SYSOFF_RUN_TIME_MISSING;
Miroslav Lichvar 0f2592
+		return -errno;
Miroslav Lichvar 0f2592
 	}
Miroslav Lichvar 0f2592
 	*result = sysoff_estimate(pso.ts, 0, n_samples, ts, delay);
Miroslav Lichvar 0f2592
-	return SYSOFF_BASIC;
Miroslav Lichvar 0f2592
+	return 0;
Miroslav Lichvar 0f2592
 }
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 int sysoff_measure(int fd, int method, int n_samples,
Miroslav Lichvar 0f2592
@@ -130,7 +131,7 @@ int sysoff_measure(int fd, int method, int n_samples,
Miroslav Lichvar 0f2592
 	case SYSOFF_BASIC:
Miroslav Lichvar 0f2592
 		return sysoff_basic(fd, n_samples, result, ts, delay);
Miroslav Lichvar 0f2592
 	}
Miroslav Lichvar 0f2592
-	return SYSOFF_RUN_TIME_MISSING;
Miroslav Lichvar 0f2592
+	return -EOPNOTSUPP;
Miroslav Lichvar 0f2592
 }
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 int sysoff_probe(int fd, int n_samples)
Miroslav Lichvar 0f2592
diff --git a/sysoff.h b/sysoff.h
Miroslav Lichvar 0f2592
index e4de919..5480f8f 100644
Miroslav Lichvar 0f2592
--- a/sysoff.h
Miroslav Lichvar 0f2592
+++ b/sysoff.h
Miroslav Lichvar 0f2592
@@ -44,7 +44,7 @@ int sysoff_probe(int fd, int n_samples);
Miroslav Lichvar 0f2592
  * @param result     The estimated offset in nanoseconds.
Miroslav Lichvar 0f2592
  * @param ts         The system time corresponding to the 'result'.
Miroslav Lichvar 0f2592
  * @param delay      The delay in reading of the clock in nanoseconds.
Miroslav Lichvar 0f2592
- * @return  One of the SYSOFF_ enumeration values.
Miroslav Lichvar 0f2592
+ * @return  Zero on success, negative error code otherwise.
Miroslav Lichvar 0f2592
  */
Miroslav Lichvar 0f2592
 int sysoff_measure(int fd, int method, int n_samples,
Miroslav Lichvar 0f2592
 		   int64_t *result, uint64_t *ts, int64_t *delay);
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
commit 25b340eb1daad807d9485728f0917ec25a376e0c
Miroslav Lichvar 0f2592
Author: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Date:   Wed May 18 10:21:29 2022 +0200
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
    sysoff: Change log level of ioctl error messages.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Change the log level of ioctl error messages to the error level to make
Miroslav Lichvar 0f2592
    them visible in default configuration, with the exception of EOPNOTSUPP
Miroslav Lichvar 0f2592
    which is expected in probing and should stay at the debug level to avoid
Miroslav Lichvar 0f2592
    confusing users.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
diff --git a/sysoff.c b/sysoff.c
Miroslav Lichvar 0f2592
index 5d3b907..a425275 100644
Miroslav Lichvar 0f2592
--- a/sysoff.c
Miroslav Lichvar 0f2592
+++ b/sysoff.c
Miroslav Lichvar 0f2592
@@ -28,6 +28,14 @@
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 #define NS_PER_SEC 1000000000LL
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
+static void print_ioctl_error(const char *name)
Miroslav Lichvar 0f2592
+{
Miroslav Lichvar 0f2592
+	if (errno == EOPNOTSUPP)
Miroslav Lichvar 0f2592
+		pr_debug("ioctl %s: %s", name, strerror(errno));
Miroslav Lichvar 0f2592
+	else
Miroslav Lichvar 0f2592
+		pr_err("ioctl %s: %s", name, strerror(errno));
Miroslav Lichvar 0f2592
+}
Miroslav Lichvar 0f2592
+
Miroslav Lichvar 0f2592
 static int64_t pctns(struct ptp_clock_time *t)
Miroslav Lichvar 0f2592
 {
Miroslav Lichvar 0f2592
 	return t->sec * NS_PER_SEC + t->nsec;
Miroslav Lichvar 0f2592
@@ -38,7 +46,7 @@ static int sysoff_precise(int fd, int64_t *result, uint64_t *ts)
Miroslav Lichvar 0f2592
 	struct ptp_sys_offset_precise pso;
Miroslav Lichvar 0f2592
 	memset(&pso, 0, sizeof(pso));
Miroslav Lichvar 0f2592
 	if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, &pso)) {
Miroslav Lichvar 0f2592
-		pr_debug("ioctl PTP_SYS_OFFSET_PRECISE: %m");
Miroslav Lichvar 0f2592
+		print_ioctl_error("PTP_SYS_OFFSET_PRECISE");
Miroslav Lichvar 0f2592
 		return -errno;
Miroslav Lichvar 0f2592
 	}
Miroslav Lichvar 0f2592
 	*result = pctns(&pso.sys_realtime) - pctns(&pso.device);
Miroslav Lichvar 0f2592
@@ -98,7 +106,7 @@ static int sysoff_extended(int fd, int n_samples,
Miroslav Lichvar 0f2592
 	memset(&pso, 0, sizeof(pso));
Miroslav Lichvar 0f2592
 	pso.n_samples = n_samples;
Miroslav Lichvar 0f2592
 	if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, &pso)) {
Miroslav Lichvar 0f2592
-		pr_debug("ioctl PTP_SYS_OFFSET_EXTENDED: %m");
Miroslav Lichvar 0f2592
+		print_ioctl_error("PTP_SYS_OFFSET_EXTENDED");
Miroslav Lichvar 0f2592
 		return -errno;
Miroslav Lichvar 0f2592
 	}
Miroslav Lichvar 0f2592
 	*result = sysoff_estimate(&pso.ts[0][0], 1, n_samples, ts, delay);
Miroslav Lichvar 0f2592
@@ -112,7 +120,7 @@ static int sysoff_basic(int fd, int n_samples,
Miroslav Lichvar 0f2592
 	memset(&pso, 0, sizeof(pso));
Miroslav Lichvar 0f2592
 	pso.n_samples = n_samples;
Miroslav Lichvar 0f2592
 	if (ioctl(fd, PTP_SYS_OFFSET, &pso)) {
Miroslav Lichvar 0f2592
-		perror("ioctl PTP_SYS_OFFSET");
Miroslav Lichvar 0f2592
+		print_ioctl_error("PTP_SYS_OFFSET");
Miroslav Lichvar 0f2592
 		return -errno;
Miroslav Lichvar 0f2592
 	}
Miroslav Lichvar 0f2592
 	*result = sysoff_estimate(pso.ts, 0, n_samples, ts, delay);
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
commit 755cf11ad6e5d02e11519b6e2644ee0f71da91ea
Miroslav Lichvar 0f2592
Author: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Date:   Thu Mar 24 12:41:49 2022 +0100
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
    sysoff: Retry on EBUSY when probing supported ioctls.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Handle EBUSY when probing support for a PTP_SYS_OFFSET ioctl. Try each
Miroslav Lichvar 0f2592
    ioctl up to three times before giving up on it to make the detection
Miroslav Lichvar 0f2592
    more reliable.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
diff --git a/sysoff.c b/sysoff.c
Miroslav Lichvar 0f2592
index a425275..fc1f7ca 100644
Miroslav Lichvar 0f2592
--- a/sysoff.c
Miroslav Lichvar 0f2592
+++ b/sysoff.c
Miroslav Lichvar 0f2592
@@ -145,8 +145,8 @@ int sysoff_measure(int fd, int method, int n_samples,
Miroslav Lichvar 0f2592
 int sysoff_probe(int fd, int n_samples)
Miroslav Lichvar 0f2592
 {
Miroslav Lichvar 0f2592
 	int64_t junk, delay;
Miroslav Lichvar 0f2592
+	int i, j, err;
Miroslav Lichvar 0f2592
 	uint64_t ts;
Miroslav Lichvar 0f2592
-	int i;
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 	if (n_samples > PTP_MAX_SAMPLES) {
Miroslav Lichvar 0f2592
 		fprintf(stderr, "warning: %d exceeds kernel max readings %d\n",
Miroslav Lichvar 0f2592
@@ -156,9 +156,15 @@ int sysoff_probe(int fd, int n_samples)
Miroslav Lichvar 0f2592
 	}
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 	for (i = 0; i < SYSOFF_LAST; i++) {
Miroslav Lichvar 0f2592
-		if (sysoff_measure(fd, i, n_samples, &junk, &ts, &delay) < 0)
Miroslav Lichvar 0f2592
-			continue;
Miroslav Lichvar 0f2592
-		return i;
Miroslav Lichvar 0f2592
+		for (j = 0; j < 3; j++) {
Miroslav Lichvar 0f2592
+			err = sysoff_measure(fd, i, n_samples, &junk, &ts,
Miroslav Lichvar 0f2592
+					     &delay);
Miroslav Lichvar 0f2592
+			if (err == -EBUSY)
Miroslav Lichvar 0f2592
+				continue;
Miroslav Lichvar 0f2592
+			if (err)
Miroslav Lichvar 0f2592
+				break;
Miroslav Lichvar 0f2592
+			return i;
Miroslav Lichvar 0f2592
+		}
Miroslav Lichvar 0f2592
 	}
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 	return SYSOFF_RUN_TIME_MISSING;
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
commit d1e8ea2405a42b42bcaf2166717fe0da6e9871ae
Miroslav Lichvar 0f2592
Author: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Date:   Tue Mar 8 12:18:31 2022 +0100
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
    phc2sys: Don't exit when reading of PHC fails with EBUSY.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Reading of the PHC can occasionally fail with some drivers, e.g. the ice
Miroslav Lichvar 0f2592
    driver returns EBUSY when it fails to get a lock. Continue in the loop
Miroslav Lichvar 0f2592
    instead of exiting on the error.
Miroslav Lichvar 0f2592
    
Miroslav Lichvar 0f2592
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 0f2592
Miroslav Lichvar 0f2592
diff --git a/phc2sys.c b/phc2sys.c
Miroslav Lichvar 0f2592
index 7a547fa..b4e2e87 100644
Miroslav Lichvar 0f2592
--- a/phc2sys.c
Miroslav Lichvar 0f2592
+++ b/phc2sys.c
Miroslav Lichvar 0f2592
@@ -623,6 +623,7 @@ static int do_pps_loop(struct phc2sys_private *priv, struct clock *clock,
Miroslav Lichvar 0f2592
 	int64_t pps_offset, phc_offset, phc_delay;
Miroslav Lichvar 0f2592
 	uint64_t pps_ts, phc_ts;
Miroslav Lichvar 0f2592
 	clockid_t src = priv->master->clkid;
Miroslav Lichvar 0f2592
+	int err;
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 	priv->master->source_label = "pps";
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
@@ -641,10 +642,13 @@ static int do_pps_loop(struct phc2sys_private *priv, struct clock *clock,
Miroslav Lichvar 0f2592
 		/* If a PHC is available, use it to get the whole number
Miroslav Lichvar 0f2592
 		   of seconds in the offset and PPS for the rest. */
Miroslav Lichvar 0f2592
 		if (src != CLOCK_INVALID) {
Miroslav Lichvar 0f2592
-			if (clockadj_compare(src, clock->clkid,
Miroslav Lichvar 0f2592
-					     priv->phc_readings,
Miroslav Lichvar 0f2592
-					     &phc_offset, &phc_ts,
Miroslav Lichvar 0f2592
-					     &phc_delay))
Miroslav Lichvar 0f2592
+			err = clockadj_compare(src, clock->clkid,
Miroslav Lichvar 0f2592
+					       priv->phc_readings,
Miroslav Lichvar 0f2592
+					       &phc_offset, &phc_ts,
Miroslav Lichvar 0f2592
+					       &phc_delay);
Miroslav Lichvar 0f2592
+			if (err == -EBUSY)
Miroslav Lichvar 0f2592
+				continue;
Miroslav Lichvar 0f2592
+			if (err)
Miroslav Lichvar 0f2592
 				return -1;
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 			/* Convert the time stamp to the PHC time. */
Miroslav Lichvar 0f2592
@@ -692,6 +696,7 @@ static int do_loop(struct phc2sys_private *priv, int subscriptions)
Miroslav Lichvar 0f2592
 	struct clock *clock;
Miroslav Lichvar 0f2592
 	uint64_t ts;
Miroslav Lichvar 0f2592
 	int64_t offset, delay;
Miroslav Lichvar 0f2592
+	int err;
Miroslav Lichvar 0f2592
 
Miroslav Lichvar 0f2592
 	interval.tv_sec = priv->phc_interval;
Miroslav Lichvar 0f2592
 	interval.tv_nsec = (priv->phc_interval - interval.tv_sec) * 1e9;
Miroslav Lichvar 0f2592
@@ -735,29 +740,32 @@ static int do_loop(struct phc2sys_private *priv, int subscriptions)
Miroslav Lichvar 0f2592
 			if (clock->clkid == CLOCK_REALTIME &&
Miroslav Lichvar 0f2592
 			    priv->master->sysoff_method >= 0) {
Miroslav Lichvar 0f2592
 				/* use sysoff */
Miroslav Lichvar 0f2592
-				if (sysoff_measure(CLOCKID_TO_FD(priv->master->clkid),
Miroslav Lichvar 0f2592
-						   priv->master->sysoff_method,
Miroslav Lichvar 0f2592
-						   priv->phc_readings,
Miroslav Lichvar 0f2592
-						   &offset, &ts, &delay) < 0)
Miroslav Lichvar 0f2592
-					return -1;
Miroslav Lichvar 0f2592
+				err = sysoff_measure(CLOCKID_TO_FD(priv->master->clkid),
Miroslav Lichvar 0f2592
+						     priv->master->sysoff_method,
Miroslav Lichvar 0f2592
+						     priv->phc_readings,
Miroslav Lichvar 0f2592
+						     &offset, &ts, &delay);
Miroslav Lichvar 0f2592
 			} else if (priv->master->clkid == CLOCK_REALTIME &&
Miroslav Lichvar 0f2592
 				   clock->sysoff_method >= 0) {
Miroslav Lichvar 0f2592
 				/* use reversed sysoff */
Miroslav Lichvar 0f2592
-				if (sysoff_measure(CLOCKID_TO_FD(clock->clkid),
Miroslav Lichvar 0f2592
-						   clock->sysoff_method,
Miroslav Lichvar 0f2592
-						   priv->phc_readings,
Miroslav Lichvar 0f2592
-						   &offset, &ts, &delay) < 0)
Miroslav Lichvar 0f2592
-					return -1;
Miroslav Lichvar 0f2592
-				offset = -offset;
Miroslav Lichvar 0f2592
-				ts += offset;
Miroslav Lichvar 0f2592
+				err = sysoff_measure(CLOCKID_TO_FD(clock->clkid),
Miroslav Lichvar 0f2592
+						     clock->sysoff_method,
Miroslav Lichvar 0f2592
+						     priv->phc_readings,
Miroslav Lichvar 0f2592
+						     &offset, &ts, &delay);
Miroslav Lichvar 0f2592
+				if (!err) {
Miroslav Lichvar 0f2592
+					offset = -offset;
Miroslav Lichvar 0f2592
+					ts += offset;
Miroslav Lichvar 0f2592
+				}
Miroslav Lichvar 0f2592
 			} else {
Miroslav Lichvar 0f2592
 				/* use phc */
Miroslav Lichvar 0f2592
-				if (clockadj_compare(priv->master->clkid,
Miroslav Lichvar 0f2592
-						     clock->clkid,
Miroslav Lichvar 0f2592
-						     priv->phc_readings,
Miroslav Lichvar 0f2592
-						     &offset, &ts, &delay))
Miroslav Lichvar 0f2592
-					return -1;
Miroslav Lichvar 0f2592
+				err = clockadj_compare(priv->master->clkid,
Miroslav Lichvar 0f2592
+						       clock->clkid,
Miroslav Lichvar 0f2592
+						       priv->phc_readings,
Miroslav Lichvar 0f2592
+						       &offset, &ts, &delay);
Miroslav Lichvar 0f2592
 			}
Miroslav Lichvar 0f2592
+			if (err == -EBUSY)
Miroslav Lichvar 0f2592
+				continue;
Miroslav Lichvar 0f2592
+			if (err)
Miroslav Lichvar 0f2592
+				return -1;
Miroslav Lichvar 0f2592
 			update_clock(priv, clock, offset, ts, delay);
Miroslav Lichvar 0f2592
 		}
Miroslav Lichvar 0f2592
 	}