From b1efe61c8e137c28077ba50d168ca896e476bf12 Mon Sep 17 00:00:00 2001
From: Adam Wujek <adam.wujek@cern.ch>
Date: Thu, 21 Jul 2016 11:45:53 +0200
Subject: [PATCH] [BUG: 1349] userspace/wrsw_hal: create shmem before locking
 the SoftPLL

Hal creates shmem before waiting for SoftPLL to lock. By this other programs
waiting for HAL's shmem can start.
Please note that after this fix PPSi does not create shmem before lock.
It waits for HALs minipc server to be ready.

Before shmem is created, read hal mode first.

Signed-off-by: Adam Wujek <adam.wujek@cern.ch>
---
 userspace/wrsw_hal/hal_main.c   | 10 ++++++++--
 userspace/wrsw_hal/hal_ports.c  |  9 ++++++---
 userspace/wrsw_hal/hal_timing.c |  9 +++++++--
 userspace/wrsw_hal/wrsw_hal.h   |  4 +++-
 4 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/userspace/wrsw_hal/hal_main.c b/userspace/wrsw_hal/hal_main.c
index cfe7c851f..157b725a3 100644
--- a/userspace/wrsw_hal/hal_main.c
+++ b/userspace/wrsw_hal/hal_main.c
@@ -110,10 +110,16 @@ static int hal_init(void)
 	/* Low-level hw init, init non-kernel drivers */
 	assert_init(shw_init());
 
+	/* read timing mode from dot-config */
+	assert_init(hal_init_timing_mode());
+
+	/* Initialize HAL's shmem - see hal_ports.c */
+	assert_init(hal_port_init_shmem(logfilename));
+
 	assert_init(hal_init_timing(logfilename));
 
-	/* Initialize port FSMs and IPC/RPC - see hal_ports.c */
-	assert_init(hal_port_init_all(logfilename));
+	/* Initialize IPC/RPC - see hal_ports.c */
+	assert_init(hal_port_init_wripc(logfilename));
 
 	//everything is fine up to here, we can blink green LED
 	shw_io_write(shw_io_led_state_o, 0);
diff --git a/userspace/wrsw_hal/hal_ports.c b/userspace/wrsw_hal/hal_ports.c
index 0de74efc0..b729d7ca3 100644
--- a/userspace/wrsw_hal/hal_ports.c
+++ b/userspace/wrsw_hal/hal_ports.c
@@ -198,7 +198,7 @@ static int hal_port_init(int index)
 
 /* Interates via all the ports defined in the config file and
  * intializes them one after another. */
-int hal_port_init_all(char *logfilename)
+int hal_port_init_shmem(char *logfilename)
 {
 	int index;
 	struct wrs_shm_head *hal_shmem_hdr;
@@ -253,10 +253,13 @@ int hal_port_init_all(char *logfilename)
 	   Especially for nports it is important */
 	wrs_shm_write(hal_shmem_hdr, WRS_SHM_WRITE_END);
 
+	return 0;
+}
+
+int hal_port_init_wripc(char *logfilename)
+{
 	/* Create a WRIPC server for HAL public API */
 	return hal_init_wripc(ports, logfilename);
-
-	return 0;
 }
 
 /* Checks if the link is up on inteface (if_name). Returns non-zero if yes. */
diff --git a/userspace/wrsw_hal/hal_timing.c b/userspace/wrsw_hal/hal_timing.c
index ee5650722..07396c4c4 100644
--- a/userspace/wrsw_hal/hal_timing.c
+++ b/userspace/wrsw_hal/hal_timing.c
@@ -20,9 +20,8 @@ static int timing_mode;
 #define LOCK_TIMEOUT_EXT 60000
 #define LOCK_TIMEOUT_INT 10000
 
-int hal_init_timing(char *filename)
+int hal_init_timing_mode(void)
 {
-	timeout_t lock_tmo;
 	static struct {
 		char *cfgname;
 		int modevalue;
@@ -39,6 +38,7 @@ int hal_init_timing(char *filename)
 		return -1;
 	}
 
+	/* Read the mode from dot-config */
 	for (m = modes; m->cfgname; m++)
 		if (libwr_cfg_get(m->cfgname))
 			break;
@@ -47,7 +47,12 @@ int hal_init_timing(char *filename)
 	if (!m->cfgname)
 		pr_error("%s: no config variable set, defaults used\n",
 			__func__);
+	return 0;
+}
 
+int hal_init_timing(char *filename)
+{
+	timeout_t lock_tmo;
 	/* initialize the RT Subsys */
 	switch (timing_mode) {
 	case HAL_TIMING_MODE_GRAND_MASTER:
diff --git a/userspace/wrsw_hal/wrsw_hal.h b/userspace/wrsw_hal/wrsw_hal.h
index 105da2a95..b9a84c7ec 100644
--- a/userspace/wrsw_hal/wrsw_hal.h
+++ b/userspace/wrsw_hal/wrsw_hal.h
@@ -16,7 +16,8 @@ int hal_config_get_string(const char *name, char *value, int max_len);
 int hal_config_iterate(const char *section, int index,
 		       char *subsection, int max_len);
 
-int hal_port_init_all(char *logfilename);
+int hal_port_init_shmem(char *logfilename);
+int hal_port_init_wripc(char *logfilename);
 void hal_port_update_all(void);
 struct hexp_port_state;
 struct hal_port_state;
@@ -30,6 +31,7 @@ int hal_port_start_lock(const char  *port_name, int priority);
 int hal_port_check_lock(const char  *port_name);
 int hal_port_enable_tracking(const char  *port_name);
 
+int hal_init_timing_mode(void);
 int hal_init_timing(char *filename);
 int hal_get_timing_mode(void);
 int hal_port_pshifter_busy(void);
-- 
GitLab