With external calibration, SFF-8472 computes temperature, voltage,
TX bias and TX power as slope * x + offset, where the slope goes up
to 255.996 and the offset is a signed 16-bit integer.
The decoder multiplied the 16-bit fields in place by the slope and
then added the offset. A result out of the field range is undefined
behavior for the float to integer conversion, and an integer sum
below zero wraps around: a bias reading of 100 with an offset of
-1000 was reported as 129.272 mA.
Compute the formula in double precision and saturate the result
to the range of the field, as already done for the RX power.
Fixes: 0caf7f376b08 ("ethdev: support SFF-8472 module telemetry")
Cc: [email protected]
Signed-off-by: Roman Khromenok <[email protected]>
---
lib/ethdev/sff_8472.c | 51 ++++++++++++++++++++++++++++---------------
1 file changed, 34 insertions(+), 17 deletions(-)
diff --git a/lib/ethdev/sff_8472.c b/lib/ethdev/sff_8472.c
index 3c73a829a8..8cd41161e3 100644
--- a/lib/ethdev/sff_8472.c
+++ b/lib/ethdev/sff_8472.c
@@ -186,6 +186,25 @@ static float befloattoh(const uint8_t *source)
return converter.dst;
}
+/* Calibrated values are stored in 16-bit fields, saturate the out of range
ones */
+static uint16_t sff_8472_cal_to_u16(double value)
+{
+ if (!(value > 0))
+ return 0;
+ if (value >= UINT16_MAX)
+ return UINT16_MAX;
+ return value;
+}
+
+static int16_t sff_8472_cal_to_s16(double value)
+{
+ if (value <= INT16_MIN)
+ return INT16_MIN;
+ if (value >= INT16_MAX)
+ return INT16_MAX;
+ return value;
+}
+
static void sff_8472_calibration(const uint8_t *data, struct sff_diags *sd)
{
unsigned long i;
@@ -195,17 +214,21 @@ static void sff_8472_calibration(const uint8_t *data,
struct sff_diags *sd)
/* Calibration should occur for all values (threshold and current) */
for (i = 0; i < RTE_DIM(sd->bias_cur); ++i) {
/*
- * Apply calibration formula 1 (Temp., Voltage, Bias, Tx Power)
+ * Apply calibration formula 1 (Temp., Voltage, Bias, Tx Power):
+ * slope * x + offset
*/
- sd->bias_cur[i] *= A2_OFFSET_TO_SLP(SFF_A2_CAL_TXI_SLP);
- sd->tx_power[i] *= A2_OFFSET_TO_SLP(SFF_A2_CAL_TXPWR_SLP);
- sd->sfp_voltage[i] *= A2_OFFSET_TO_SLP(SFF_A2_CAL_V_SLP);
- sd->sfp_temp[i] *= A2_OFFSET_TO_SLP(SFF_A2_CAL_T_SLP);
-
- sd->bias_cur[i] += A2_OFFSET_TO_OFF(SFF_A2_CAL_TXI_OFF);
- sd->tx_power[i] += A2_OFFSET_TO_OFF(SFF_A2_CAL_TXPWR_OFF);
- sd->sfp_voltage[i] += A2_OFFSET_TO_OFF(SFF_A2_CAL_V_OFF);
- sd->sfp_temp[i] += A2_OFFSET_TO_OFF(SFF_A2_CAL_T_OFF);
+ sd->bias_cur[i] = sff_8472_cal_to_u16(sd->bias_cur[i] *
+ A2_OFFSET_TO_SLP(SFF_A2_CAL_TXI_SLP) +
+ A2_OFFSET_TO_OFF(SFF_A2_CAL_TXI_OFF));
+ sd->tx_power[i] = sff_8472_cal_to_u16(sd->tx_power[i] *
+ A2_OFFSET_TO_SLP(SFF_A2_CAL_TXPWR_SLP) +
+ A2_OFFSET_TO_OFF(SFF_A2_CAL_TXPWR_OFF));
+ sd->sfp_voltage[i] = sff_8472_cal_to_u16(sd->sfp_voltage[i] *
+ A2_OFFSET_TO_SLP(SFF_A2_CAL_V_SLP) +
+ A2_OFFSET_TO_OFF(SFF_A2_CAL_V_OFF));
+ sd->sfp_temp[i] = sff_8472_cal_to_s16(sd->sfp_temp[i] *
+ A2_OFFSET_TO_SLP(SFF_A2_CAL_T_SLP) +
+ A2_OFFSET_TO_OFF(SFF_A2_CAL_T_OFF));
/*
* Apply calibration formula 2 (Rx Power only):
@@ -219,13 +242,7 @@ static void sff_8472_calibration(const uint8_t *data,
struct sff_diags *sd)
rx_power = rx_power * rx_reading +
A2_OFFSET_TO_RXPWRx(SFF_A2_CAL_RXPWR1);
rx_power = rx_power * rx_reading +
A2_OFFSET_TO_RXPWRx(SFF_A2_CAL_RXPWR0);
- /* the result is stored in 0.1 uW units, out of range is not
representable */
- if (!(rx_power > 0))
- sd->rx_power[i] = 0;
- else if (rx_power >= UINT16_MAX)
- sd->rx_power[i] = UINT16_MAX;
- else
- sd->rx_power[i] = rx_power;
+ sd->rx_power[i] = sff_8472_cal_to_u16(rx_power);
}
}
--
2.47.3