net: phy: microchip_t1: SQI support for LAN887x
PATCH v3 1/1 net: phy: microchip_t1: SQI support for LAN887x
From: Tarun Alle <[email protected]>
Add support for measuring Signal Quality Index for LAN887x T1 PHY. Signal Quality Index (SQI) is measure of Link Channel Quality from 0 to 7, with 7 as the best. By default, a link loss event shall indicate an SQI of 0.
---
v2 -> v3
Addressed below review comment
- Replaced hard-coded values with ARRAY_SIZE(rawtable).
---
v1 -> v2
Addressed below review comments
- Replaced hard-coded 200 with ARRAY_SIZE(rawtable).
- Replaced return value -EINVAL with -ENETDOWN.
- Changed link checks.
--- drivers/net/phy/microchip_t1.c | 155 +++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+)
drivers/net/phy/microchip_t1.c +155 -0
@@ -2,6 +2,7 @@// Copyright (C) 2018 Microchip Technology #include <linux/kernel.h> +#include <linux/sort.h> #include <linux/module.h> #include <linux/delay.h> #include <linux/mii.h>
@@ -188,6 +189,20 @@#define LAN887X_EFUSE_READ_DAT9_SGMII_DIS BIT(9)#define LAN887X_EFUSE_READ_DAT9_MAC_MODE GENMASK(1, 0) +#define LAN887X_COEFF_PWR_DN_CONFIG_100 0x0404 +#define LAN887X_COEFF_PWR_DN_CONFIG_100_V 0x16d6 +#define LAN887X_SQI_CONFIG_100 0x042e +#define LAN887X_SQI_CONFIG_100_V 0x9572 +#define LAN887X_SQI_MSE_100 0x483 + +#define LAN887X_POKE_PEEK_100 0x040d +#define LAN887X_POKE_PEEK_100_EN BIT(0) + +#define LAN887X_COEFF_MOD_CONFIG 0x080d +#define LAN887X_COEFF_MOD_CONFIG_DCQ_COEFF_EN BIT(8) + +#define LAN887X_DCQ_SQI_STATUS 0x08b2 +#define DRIVER_AUTHOR "Nisar Sayed <[email protected]>"#define DRIVER_DESC "Microchip LAN87XX/LAN937x/LAN887x T1 PHY driver"
@@ -1420,6 +1435,144 @@ static void lan887x_get_strings(struct phy_device *phydev, u8 *data)ethtool_puts (&data, lan887x_hw_stats[i].string); } +/* Compare block to sort in ascending order */ +static int data_compare (const void *a,const void *b) +{ +return *(u16 *)a - *(u16 *)b; +} + +static int lan887x_get_sqi_100M (struct phy_device *phydev) +{ +u16 rawtable[200 ]; +u32 sqiavg =0 ; +u8 sqinum; +int rc; + +/* Configuration of SQI 100M */ + rc =phy_write_mmd (phydev,MDIO_MMD_VEND1 , +LAN887X_COEFF_PWR_DN_CONFIG_100 , +LAN887X_COEFF_PWR_DN_CONFIG_100_V ); +if (rc <0 ) +return rc; + + rc =phy_write_mmd (phydev,MDIO_MMD_VEND1 ,LAN887X_SQI_CONFIG_100 , +LAN887X_SQI_CONFIG_100_V ); +if (rc <0 ) +return rc; + + rc =phy_read_mmd (phydev,MDIO_MMD_VEND1 ,LAN887X_SQI_CONFIG_100 ); +if (rc !=LAN887X_SQI_CONFIG_100_V ) +return -EINVAL ; + + rc =phy_modify_mmd (phydev,MDIO_MMD_VEND1 ,LAN887X_POKE_PEEK_100 , +LAN887X_POKE_PEEK_100_EN , +LAN887X_POKE_PEEK_100_EN ); +if (rc <0 ) +return rc; + +/* Required before reading register +* otherwise it will return high value +*/ +msleep (50 ); + +/* Link check before raw readings */ + rc =genphy_c45_read_link (phydev); +if (rc <0 ) +return rc; + +if (!phydev->link) +return -ENETDOWN ; + +/* Get 200 SQI raw readings */ +for (int i =0 ; i <ARRAY_SIZE (rawtable); i++) { + rc =phy_write_mmd (phydev,MDIO_MMD_VEND1 , +LAN887X_POKE_PEEK_100 , +LAN887X_POKE_PEEK_100_EN ); +if (rc <0 ) +return rc; + + rc =phy_read_mmd (phydev,MDIO_MMD_VEND1 , +LAN887X_SQI_MSE_100 ); +if (rc <0 ) +return rc; + + rawtable[i] = rc; + } + +/* Link check after raw readings */ + rc =genphy_c45_read_link (phydev); +if (rc <0 ) +return rc; + +if (!phydev->link) +return -ENETDOWN ; + +/* Sort SQI raw readings in ascending order */ +sort (rawtable,ARRAY_SIZE (rawtable),sizeof (u16), data_compare,NULL ); + +/* Keep inliers and discard outliers */ +for (int i =ARRAY_SIZE (rawtable) /5 ; + i <ARRAY_SIZE (rawtable) /5 *4 ; i++) + sqiavg += rawtable[i]; + +/* Get SQI average */ + sqiavg /=120 ; + +if (sqiavg <75 ) + sqinum =7 ; +else if (sqiavg <94 ) + sqinum =6 ; +else if (sqiavg <119 ) + sqinum =5 ; +else if (sqiavg <150 ) + sqinum =4 ; +else if (sqiavg <189 ) + sqinum =3 ; +else if (sqiavg <237 ) + sqinum =2 ; +else if (sqiavg <299 ) + sqinum =1 ; +else + sqinum =0 ; + +return sqinum; +} + +static int lan887x_get_sqi (struct phy_device *phydev) +{ +int rc, val; + +if (phydev->speed !=SPEED_1000 && + phydev->speed !=SPEED_100 ) { +return -ENETDOWN ; + } + +if (phydev->speed ==SPEED_100 ) +return lan887x_get_sqi_100M (phydev); + +/* Writing DCQ_COEFF_EN to trigger a SQI read */ + rc =phy_set_bits_mmd (phydev,MDIO_MMD_VEND1 , +LAN887X_COEFF_MOD_CONFIG , +LAN887X_COEFF_MOD_CONFIG_DCQ_COEFF_EN ); +if (rc <0 ) +return rc; + +/* Wait for DCQ done */ + rc =phy_read_mmd_poll_timeout (phydev,MDIO_MMD_VEND1 , +LAN887X_COEFF_MOD_CONFIG , val, ((val & +LAN887X_COEFF_MOD_CONFIG_DCQ_COEFF_EN ) != +LAN887X_COEFF_MOD_CONFIG_DCQ_COEFF_EN ), +10 ,200 , true); +if (rc <0 ) +return rc; + + rc =phy_read_mmd (phydev,MDIO_MMD_VEND1 ,LAN887X_DCQ_SQI_STATUS ); +if (rc <0 ) +return rc; + +return FIELD_GET (T1_DCQ_SQI_MSK , rc); +} +static struct phy_driver microchip_t1_phy_driver[] = { {PHY_ID_MATCH_MODEL (PHY_ID_LAN87XX ),
@@ -1468,6 +1621,8 @@ static struct phy_driver microchip_t1_phy_driver[] = { .suspend = genphy_suspend, .resume = genphy_resume, .read_status = genphy_c45_read_status, + .get_sqi = lan887x_get_sqi, + .get_sqi_max = lan87xx_get_sqi_max, } };
Signature · 1 line
2.34.1
Andrew Lunn
maintainerdrivers/net/phy/microchip_t1.c static void lan887x_get_strings(struct phy_device *phydev, u8 *data) · line 1519
Quoted 7 lines
drivers/net/phy/microchip_t1.c +7
-0+/* Keep inliers and discard outliers */ +for (int i =ARRAY_SIZE (rawtable) /5 ; + i <ARRAY_SIZE (rawtable) /5 *4 ; i++) + sqiavg += rawtable[i]; + +/* Get SQI average */ + sqiavg /=120 ;
120?
Isn't that ARRAY_SIZE(rawtable) / 5 * 4 - ARRAY_SIZE(rawtable) / 5
Please think about the comments being given. I said you should not assume 200, but use ARRAY_SIZE, so it is possible to change the size of the array and not get buffer overruns etc. So you need to review all the code.
Better still, change it to 50 and make sure you get sensible values from it. The accuracy won't be as good, but i would expect it to be still about right. But with the current code, i guess you get 7 no matter what the actual quality is.
This is a general principle in C code, and coding in general. Don't scatter the same knowledge repeatedly everywhere, because it makes it error prone to change. You have to find and change them all, rather than just one central value.
Andrew
---
pw-bot: cr
Hi Andrew,
Thank you for the review comments.
Quoted 23 lines
-----Original Message-----
From: Andrew Lunn <[email protected]>
Sent: Wednesday, September 11, 2024 10:18 PM
To: Tarun Alle - I68638 <[email protected]>
Cc: Arun Ramadoss - I17769 <[email protected]>; UNGLinuxDriver <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH net-next V3] net: phy:microchip_t1: SQI support for LAN887xEXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
drivers/net/phy/microchip_t1.c static void lan887x_get_strings(struct phy_device *phydev, u8 *data) · line 1519
Quoted 7 lines
drivers/net/phy/microchip_t1.c +7
-0+/* Keep inliers and discard outliers */ +for (int i =ARRAY_SIZE (rawtable) /5 ; + i <ARRAY_SIZE (rawtable) /5 *4 ; i++) + sqiavg += rawtable[i]; + +/* Get SQI average */ + sqiavg /=120 ;
Quoted 8 lines
120?
Isn't that
ARRAY_SIZE(rawtable)/ 5 * 4 -ARRAY_SIZE(rawtable)/ 5Please think about the comments being given. I said you should not assume 200, but use
ARRAY_SIZE, so it is possible to change the size of the array and not get buffer overruns etc. So you need to review all the code.
My apologies I realized the issue just after sending the patch.
Quoted 4 lines
Better still, change it to 50 and make sure you get sensible values from it. The accuracy won't be as good, but i would expect it to be still about right. But with the current code, i guess you get 7 no matter what the actual quality is.
To be consistent and accurate with compliance tests, 200 samples are used. If customer wants accurate SQI value, we need to use suggested sample count as per the compliance reports. Otherwise, single (any other value) sample can also be used which will give ~ +/-1 SQI value (which may not be guaranteed always).
Quoted 4 lines
This is a general principle in C code, and coding in general. Don't scatter the same knowledge repeatedly everywhere, because it makes it error prone to change. You have to find and change them all, rather than just one central value.
Going forward I will take care of constants and repetitive calculations.
Quoted 4 lines
Andrew
---
pw-bot: cr
Thanks,
Tarun Alle.