Commit 9bb39e98 authored by Alessandro Rubini's avatar Alessandro Rubini

userspace: lib and hal: use new dot-config instead of sfp-database.conf

This commit uses dot-config (previous commit), in exactly the same way
as previous code. No change in behaviour is there, as confirmed by
looking at shared memory values.

However, the alpha value is not part of the SFP definition any more,
but it is part of the fiber type (later commits).  So it is currently
hardwired in the code, matching previous config values.

The data structures are slightly changed, so the version in shared memory
is increased too.  wrs_dump_shmem is updated at the same time
and ppsi is updated to get the new headers.

As a side effect, this uses calloc() instead of malloc(), and sets a
sensible (though unused) value in the "flags" field of the sfp data.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 77efb585
......@@ -16,6 +16,7 @@
#include <libwr/pio.h>
#include <libwr/trace.h>
#include <libwr/util.h>
#include <libwr/config.h>
#include "i2c.h"
#include "i2c_sfp.h"
......@@ -547,70 +548,79 @@ int shw_sfp_read_verify_header(int num, struct shw_sfp_header *head)
static struct shw_sfp_caldata *shw_sfp_cal_list = NULL;
int shw_sfp_read_db(char *filename)
/* local helper */
static void __err_msg(int index, char *pname, char *pvalue)
{
lua_State *L = luaL_newstate();
struct shw_sfp_caldata *sfp;
luaL_openlibs(L);
if (luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0)) {
printf("cannot run configuration file: %s",
lua_tostring(L, -1));
return -1;
}
fprintf(stderr, "Config item \"SFP%02i_PARAMS\": parameter \"%s\" ",
index, pname);
if (pvalue)
fprintf(stderr, "is wrong (\"%s\")\n", pvalue);
else
fprintf(stderr, "is not specified\n");
}
lua_getglobal(L, "sfpdb");
if (!lua_istable(L, -1)) {
printf("`sfpdb' should be a table\n");
return -1;
}
lua_pushnil(L);
while (lua_next(L, -2)) {
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
continue;
int shw_sfp_read_db(void)
{
struct shw_sfp_caldata *sfp;
char s[128];
int error, val, index;
for (index = 0; ; index++) {
error = libwr_cfg_convert2("SFP%02i_PARAMS", "name",
LIBWR_STRING, s, index);
if (error)
return 0; /* no more, no error */
sfp = calloc(1, sizeof(*sfp));
strncpy(sfp->part_num, s, sizeof(sfp->part_num));
sfp->vendor_serial[0] = 0;
sfp->flags = SFP_FLAG_CLASS_DATA; /* never used */
/* These are uint32_t as I write this. So use "int val" */
val = 0;
error = libwr_cfg_convert2("SFP%02i_PARAMS", "tx",
LIBWR_INT, &val, index);
if (error)
__err_msg(index, "tx", NULL);
sfp->delta_tx = val;
val = 0;
error = libwr_cfg_convert2("SFP%02i_PARAMS", "rx",
LIBWR_INT, &val, index);
if (error)
__err_msg(index, "rx", NULL);
sfp->delta_rx = val;
/* We also store the wavelength, used to get alpha */
error = libwr_cfg_convert2("SFP%02i_PARAMS", "wl_txrx",
LIBWR_STRING, &s, index);
if (error)
__err_msg(index, "wl_txrx", NULL);
if (sscanf(s, "%i+%i", &sfp->tx_wl, &sfp->rx_wl) != 2) {
sfp->tx_wl = 0;
__err_msg(index, "wl_txrx", s);
}
const char *sfp_pn = 0;
const char *sfp_vs = 0;
int vals[2] = { 0, 0 };
double alpha = 0;
lua_pushnil(L);
while (lua_next(L, -2)) {
const char *key = lua_tostring(L, -2);
if (strcmp(key, "part_num") == 0)
sfp_pn = lua_tostring(L, -1);
else if (strcmp(key, "part_serial") == 0)
sfp_vs = lua_tostring(L, -1);
else if (strcmp(key, "alpha") == 0)
alpha = lua_tonumber(L, -1);
else if (strcmp(key, "delta_tx") == 0)
vals[0] = lua_tointeger(L, -1);
else if (strcmp(key, "delta_rx") == 0)
vals[1] = lua_tointeger(L, -1);
lua_pop(L, 1);
/*
* Now, alpha is missing. We need to hardwire one, before
* we add the fiber type so to pick it, instead
*/
sfp->alpha = 0.0;
if (sfp->tx_wl == 1310 && sfp->rx_wl == 1490)
sfp->alpha = 2.67871791665542e-04;
if (sfp->tx_wl == 1490 && sfp->rx_wl == 1310)
sfp->alpha = -2.67800055584799e-04;
if (sfp->alpha == 0.0) {
fprintf(stderr, "SFP%02i_PARAMS: Unexpected wl pair "
"(tx %i, rx %i)\n", index,
sfp->tx_wl, sfp->rx_wl);
/* Use DEFAULT_FIBER_ALPHA_COEF from hal_ports.c */
sfp->alpha = (1.4682e-04*1.76);
}
lua_pop(L, 1);
sfp = malloc(sizeof(struct shw_sfp_caldata));
strncpy(sfp->part_num, sfp_pn, sizeof(sfp->part_num));
if (!sfp_vs || strcmp(sfp_vs, "") == 0) {
sfp->vendor_serial[0] = 0;
sfp->flags |= SFP_FLAG_CLASS_DATA;
} else {
strcpy(sfp->vendor_serial, sfp_vs);
sfp->flags |= SFP_FLAG_DEVICE_DATA;
}
sfp->alpha = alpha;
sfp->delta_tx = vals[0];
sfp->delta_rx = vals[1];
/* link and continue */
sfp->next = shw_sfp_cal_list;
shw_sfp_cal_list = sfp;
}
lua_pop(L, 1);
lua_close(L);
return 0;
}
......
......@@ -92,7 +92,7 @@ struct hal_port_state {
};
/* This is the overall structure stored in shared memory */
#define HAL_SHMEM_VERSION 1
#define HAL_SHMEM_VERSION 2 /* Version 2 because sfp calib changed */
struct hal_shmem_header {
int nports;
struct hal_port_state *ports;
......
......@@ -23,12 +23,21 @@
struct shw_sfp_caldata {
int flags;
char part_num[16]; /* part number of device as found in DB */
/*
* Part number used to identify it. Serial number because we
* may specify per-specimen delays, but it is not used at this
* point in time
*/
char part_num[16];
char vendor_serial[16];
/* Callibration data */
double alpha;
uint32_t delta_tx;
uint32_t delta_rx;
/* wavelengths, used to get alpha from fiber type */
int tx_wl;
int rx_wl;
/* and link as a list */
struct shw_sfp_caldata *next;
};
......@@ -87,8 +96,8 @@ static inline void shw_sfp_set_generic(int num, int status, int type)
shw_sfp_gpio_set(num, state);
}
/* Load the db from a file */
int shw_sfp_read_db(char *filename);
/* Load the db from dot-config to internal structures */
int shw_sfp_read_db(void);
/* Read and verify the header all at once. returns -1 on failure */
int shw_sfp_read_verify_header(int num, struct shw_sfp_header *head);
......
ppsi @ 12bd4376
Subproject commit f49685ec07132da80b8aec32a2d50d143d23e13d
Subproject commit 12bd4376a1eed963eed143cda3e140cc55cb7ccb
......@@ -207,7 +207,9 @@ struct dump_info hal_port_info [] = {
DUMP_FIELD(double, calib.sfp.alpha),
DUMP_FIELD(uint32_t, calib.sfp.delta_tx),
DUMP_FIELD(uint32_t, calib.sfp.delta_rx),
DUMP_FIELD(pointer, calib.sfp.next),
DUMP_FIELD(int, calib.sfp.tx_wl),
DUMP_FIELD(int, calib.sfp.rx_wl),
DUMP_FIELD(pointer, calib.sfp.next),
DUMP_FIELD(uint32_t, phase_val),
DUMP_FIELD(int, phase_val_valid),
......
......@@ -73,8 +73,6 @@ static void hal_deamonize();
/* Main initialization function */
static int hal_init()
{
char sfp_db_path[1024];
//trace_log_stderr();
TRACE(TRACE_INFO, "HAL initializing...");
......@@ -82,6 +80,7 @@ static int hal_init()
memset(cleanup_cb, 0, sizeof(cleanup_cb));
libwr_cfg_read_file("/wr/etc/dot-config"); /* FIXME: accept -f */
shw_sfp_read_db();
/* Set up trap for some signals - the main purpose is to
prevent the hardware from working when the HAL is shut down
......@@ -94,17 +93,6 @@ static int hal_init()
assert_init(hal_parse_config());
if (!hal_config_get_string("global.sfp_database_path",
sfp_db_path, sizeof(sfp_db_path))) {
if (shw_sfp_read_db(sfp_db_path) < 0) {
TRACE(TRACE_ERROR, "Can't read SFP database (%s)",
sfp_db_path);
} else {
TRACE(TRACE_INFO, "Loaded SFP database (%s)",
sfp_db_path);
}
}
/* Low-level hw init, init non-kernel drivers */
assert_init(shw_init());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment