From c7d6adde6b08eff23b8bd7551e8a5af7ebd37628 Mon Sep 17 00:00:00 2001
From: Andrew Bower <andrew.bower@amd.com>
Date: Sat, 16 Mar 2024 18:59:38 +0000
Subject: [PATCH] clock: avoid checking phc state for system clock

clock->u.nic.phc happens typically to be non-zero for a system clock type in
most systems but apparently not on Raspberry Pi 5 with Debian 12. It could be
a layout change or system clock characteristics change but in any case the
code should not have been checking this field. Fixes Xilinx-CNS/sfptpd#9
---
 src/sfptpd_clock.c | 9 ++++++---
 src/sfptpd_phc.c   | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/sfptpd_clock.c b/src/sfptpd_clock.c
index 03b8fc5a..2dfee86f 100644
--- a/src/sfptpd_clock.c
+++ b/src/sfptpd_clock.c
@@ -1621,7 +1621,8 @@ int sfptpd_clock_adjust_time(struct sfptpd_clock *clock, struct timespec *offset
 	assert(clock->magic == SFPTPD_CLOCK_MAGIC);
 	assert(offset != NULL);
 
-	if (clock->u.nic.phc == NULL) {
+	if (clock->type != SFPTPD_CLOCK_TYPE_SYSTEM &&
+	    clock->u.nic.phc == NULL) {
 		ERROR("clock %s: unable to step clock - no phc device\n",
 		      clock->long_name);
 		rc = ENODEV;
@@ -1683,7 +1684,8 @@ int sfptpd_clock_adjust_frequency(struct sfptpd_clock *clock, long double freq_a
 	assert(clock != NULL);
 	assert(clock->magic == SFPTPD_CLOCK_MAGIC);
 
-	if (clock->u.nic.phc == NULL) {
+	if (clock->type != SFPTPD_CLOCK_TYPE_SYSTEM &&
+	    clock->u.nic.phc == NULL) {
 		ERROR("clock %s: unable to adjust frequency - no phc device\n",
 		      clock->long_name);
 		rc = ENODEV;
@@ -1877,7 +1879,8 @@ int sfptpd_clock_get_time(const struct sfptpd_clock *clock, struct timespec *tim
 	assert(clock->magic == SFPTPD_CLOCK_MAGIC);
 	assert(time != NULL);
 
-	if (clock->u.nic.phc == NULL) {
+	if (clock->type != SFPTPD_CLOCK_TYPE_SYSTEM &&
+	    clock->u.nic.phc == NULL) {
 		ERROR("clock %s: unable to get time - no phc device\n",
 		      clock->long_name);
 		rc = ENODEV;
diff --git a/src/sfptpd_phc.c b/src/sfptpd_phc.c
index 0a486de9..c0e3086c 100644
--- a/src/sfptpd_phc.c
+++ b/src/sfptpd_phc.c
@@ -341,7 +341,7 @@ static int phc_configure_pps(struct sfptpd_phc *phc)
 
 	if (ioctl(phc->phc_fd, PTP_ENABLE_PPS, 1) != 0) {
 		ERROR("phc%d: failed to enable PPS events, %s\n",
-		      phc->phc_fd, strerror(errno));
+		      phc->phc_idx, strerror(errno));
 		rc = errno;
 		goto fail2;
 	}