When running in the automatic mode, get the PHC index of the port
from PORT_HWCLOCK_NP instead of calling sk_get_ts_info(). This allows
phc2sys -a to synchronize (to) a virtual clock.

Signed-off-by: Miroslav Lichvar <mlich...@redhat.com>
---
 phc2sys.c   | 42 +++++++++++++++++++++++++-----------------
 pmc_agent.c | 18 +++++++++++++++++-
 pmc_agent.h |  4 +++-
 3 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/phc2sys.c b/phc2sys.c
index 489e15b..15dd689 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -153,14 +153,21 @@ static struct servo *servo_add(struct phc2sys_private 
*priv,
        return servo;
 }
 
-static struct clock *clock_add(struct phc2sys_private *priv, const char 
*device)
+static struct clock *clock_add(struct phc2sys_private *priv, const char 
*device,
+                              int phc_index)
 {
        struct clock *c;
        clockid_t clkid = CLOCK_INVALID;
-       int phc_index = -1;
+       char phc_device[19];
 
        if (device) {
-               clkid = posix_clock_open(device, &phc_index);
+               if (phc_index >= 0) {
+                       snprintf(phc_device, sizeof(phc_device), "/dev/ptp%d",
+                                phc_index);
+                       clkid = posix_clock_open(phc_device, &phc_index);
+               } else {
+                       clkid = posix_clock_open(device, &phc_index);
+               }
                if (clkid == CLOCK_INVALID)
                        return NULL;
        }
@@ -257,7 +264,7 @@ static struct port *port_get(struct phc2sys_private *priv, 
unsigned int number)
 }
 
 static struct port *port_add(struct phc2sys_private *priv, unsigned int number,
-                            char *device)
+                            char *device, int phc_index)
 {
        struct port *p;
        struct clock *c = NULL, *tmp;
@@ -274,7 +281,7 @@ static struct port *port_add(struct phc2sys_private *priv, 
unsigned int number,
                }
        }
        if (!c) {
-               c = clock_add(priv, device);
+               c = clock_add(priv, device, phc_index);
                if (!c)
                        return NULL;
        }
@@ -293,9 +300,8 @@ static void clock_reinit(struct phc2sys_private *priv, 
struct clock *clock,
                         int new_state)
 {
        int err = -1, phc_index = -1, phc_switched = 0, state, timestamping;
+       char iface[IFNAMSIZ], phc_device[19];
        struct port *p;
-       struct sk_ts_info ts_info;
-       char iface[IFNAMSIZ];
        clockid_t clkid = CLOCK_INVALID;
 
        LIST_FOREACH(p, &priv->ports, list) {
@@ -304,7 +310,8 @@ static void clock_reinit(struct phc2sys_private *priv, 
struct clock *clock,
                }
                err = pmc_agent_query_port_properties(priv->agent, 1000,
                                                      p->number, &state,
-                                                     &timestamping, iface);
+                                                     &timestamping, &phc_index,
+                                                     iface);
                if (!err) {
                        p->state = normalize_state(state);
                }
@@ -318,9 +325,10 @@ static void clock_reinit(struct phc2sys_private *priv, 
struct clock *clock,
                        clock->device = strdup(iface);
                }
                /* Check if phc index changed */
-               if (!sk_get_ts_info(clock->device, &ts_info) &&
-                   clock->phc_index != ts_info.phc_index) {
-                       clkid = posix_clock_open(clock->device, &phc_index);
+               if (clock->phc_index != phc_index) {
+                       snprintf(phc_device, sizeof(phc_device), "/dev/ptp%d",
+                                phc_index);
+                       clkid = posix_clock_open(phc_device, &phc_index);
                        if (clkid == CLOCK_INVALID)
                                return;
 
@@ -816,7 +824,7 @@ static int phc2sys_recv_subscribed(void *context, struct 
ptp_message *msg,
 
 static int auto_init_ports(struct phc2sys_private *priv, int add_rt)
 {
-       int err, number_ports, state, timestamping;
+       int err, number_ports, phc_index, state, timestamping;
        char iface[IFNAMSIZ];
        struct clock *clock;
        struct port *port;
@@ -852,7 +860,7 @@ static int auto_init_ports(struct phc2sys_private *priv, 
int add_rt)
        for (i = 1; i <= number_ports; i++) {
                err = pmc_agent_query_port_properties(priv->agent, 1000, i,
                                                      &state, &timestamping,
-                                                     iface);
+                                                     &phc_index, iface);
                if (err == -ENODEV) {
                        /* port does not exist, ignore the port */
                        continue;
@@ -865,7 +873,7 @@ static int auto_init_ports(struct phc2sys_private *priv, 
int add_rt)
                        /* ignore ports with software time stamping */
                        continue;
                }
-               port = port_add(priv, i, iface);
+               port = port_add(priv, i, iface, phc_index);
                if (!port)
                        return -1;
                port->state = normalize_state(state);
@@ -880,7 +888,7 @@ static int auto_init_ports(struct phc2sys_private *priv, 
int add_rt)
        priv->state_changed = 1;
 
        if (add_rt) {
-               clock = clock_add(priv, "CLOCK_REALTIME");
+               clock = clock_add(priv, "CLOCK_REALTIME", -1);
                if (!clock)
                        return -1;
                if (add_rt == 1)
@@ -963,7 +971,7 @@ static int phc2sys_static_configuration(struct 
phc2sys_private *priv,
 {
        struct clock *src, *dst;
 
-       src = clock_add(priv, src_name);
+       src = clock_add(priv, src_name, -1);
        if (!src) {
                fprintf(stderr, "valid source clock must be selected.\n");
                return -1;
@@ -971,7 +979,7 @@ static int phc2sys_static_configuration(struct 
phc2sys_private *priv,
        src->state = PS_SLAVE;
        priv->master = src;
 
-       dst = clock_add(priv, dst_name);
+       dst = clock_add(priv, dst_name, -1);
        if (!dst) {
                fprintf(stderr, "valid destination clock must be selected.\n");
                return -1;
diff --git a/pmc_agent.c b/pmc_agent.c
index 86350d8..79971e2 100644
--- a/pmc_agent.c
+++ b/pmc_agent.c
@@ -304,9 +304,10 @@ int pmc_agent_query_dds(struct pmc_agent *node, int 
timeout)
 
 int pmc_agent_query_port_properties(struct pmc_agent *node, int timeout,
                                    unsigned int port, int *state,
-                                   int *tstamping, char *iface)
+                                   int *tstamping, int *phc_index, char *iface)
 {
        struct port_properties_np *ppn;
+       struct port_hwclock_np *phn;
        struct ptp_message *msg;
        int res, len;
 
@@ -330,6 +331,21 @@ int pmc_agent_query_port_properties(struct pmc_agent 
*node, int timeout,
                memcpy(iface, ppn->interface.text, len);
                iface[len] = '\0';
 
+               msg_put(msg);
+               break;
+       }
+       while (1) {
+               res = run_pmc(node, timeout, MID_PORT_HWCLOCK_NP, &msg);
+               if (is_run_pmc_error(res)) {
+                       goto out;
+               }
+               phn = management_tlv_data(msg);
+               if (phn->portIdentity.portNumber != port) {
+                       msg_put(msg);
+                       continue;
+               }
+               *phc_index = phn->phc_index;
+
                msg_put(msg);
                res = RUN_PMC_OKAY;
                break;
diff --git a/pmc_agent.h b/pmc_agent.h
index 11b9347..ad4b771 100644
--- a/pmc_agent.h
+++ b/pmc_agent.h
@@ -103,12 +103,14 @@ int pmc_agent_query_dds(struct pmc_agent *agent, int 
timeout);
  * @param port       The port index of interest.
  * @param state      Buffer to hold the returned port state.
  * @param tstamping  Buffer to hold the returned time stamping flavor.
+ * @param phc_index  Buffer to hold the returned PHC index.
  * @param iface      Buffer to hold the returned interface name.
  * @return           Zero on success, negative error code otherwise.
  */
 int pmc_agent_query_port_properties(struct pmc_agent *agent, int timeout,
                                    unsigned int port, int *state,
-                                   int *tstamping, char *iface);
+                                   int *tstamping, int *phc_index,
+                                   char *iface);
 
 /**
  * Queries the TAI-UTC offset and the current leap adjustment from the
-- 
2.35.1



_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to