Skip to content

Commit 2a330b5

Browse files
committed
Merge branch 'provide-KAPI-for-SQI'
Oleksij Rempel says: ==================== provide KAPI for SQI This patches are extending ethtool netlink interface to export Signal Quality Index (SQI). SQI provided by 100Base-T1 PHYs and can be used for cable diagnostic. Compared to a typical cable tests, this value can be only used after link is established. changes v3: - rename __ethtool_get_sqi* to linkstate_get_sqi*. And move this functions to the net/ethtool/linkstate.c - protect linkstate_get_sqi* with locking changes v2: - use u32 instead of u8 for SQI - add SQI_MAX field and callbacks - some style fixes in the rst. - do not convert index to shifted index. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents b0301a5 + 68ff5e1 commit 2a330b5

File tree

5 files changed

+108
-3
lines changed

5 files changed

+108
-3
lines changed

Documentation/networking/ethtool-netlink.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,12 @@ Request contents:
454454

455455
Kernel response contents:
456456

457-
==================================== ====== ==========================
457+
==================================== ====== ============================
458458
``ETHTOOL_A_LINKSTATE_HEADER`` nested reply header
459459
``ETHTOOL_A_LINKSTATE_LINK`` bool link state (up/down)
460-
==================================== ====== ==========================
460+
``ETHTOOL_A_LINKSTATE_SQI`` u32 Current Signal Quality Index
461+
``ETHTOOL_A_LINKSTATE_SQI_MAX`` u32 Max support SQI value
462+
==================================== ====== ============================
461463

462464
For most NIC drivers, the value of ``ETHTOOL_A_LINKSTATE_LINK`` returns
463465
carrier flag provided by ``netif_carrier_ok()`` but there are drivers which

drivers/net/phy/nxp-tja11xx.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353

5454
#define MII_COMMSTAT 23
5555
#define MII_COMMSTAT_LINK_UP BIT(15)
56+
#define MII_COMMSTAT_SQI_STATE GENMASK(7, 5)
57+
#define MII_COMMSTAT_SQI_MAX 7
5658

5759
#define MII_GENSTAT 24
5860
#define MII_GENSTAT_PLL_LOCKED BIT(14)
@@ -329,6 +331,22 @@ static int tja11xx_read_status(struct phy_device *phydev)
329331
return 0;
330332
}
331333

334+
static int tja11xx_get_sqi(struct phy_device *phydev)
335+
{
336+
int ret;
337+
338+
ret = phy_read(phydev, MII_COMMSTAT);
339+
if (ret < 0)
340+
return ret;
341+
342+
return FIELD_GET(MII_COMMSTAT_SQI_STATE, ret);
343+
}
344+
345+
static int tja11xx_get_sqi_max(struct phy_device *phydev)
346+
{
347+
return MII_COMMSTAT_SQI_MAX;
348+
}
349+
332350
static int tja11xx_get_sset_count(struct phy_device *phydev)
333351
{
334352
return ARRAY_SIZE(tja11xx_hw_stats);
@@ -683,6 +701,8 @@ static struct phy_driver tja11xx_driver[] = {
683701
.config_aneg = tja11xx_config_aneg,
684702
.config_init = tja11xx_config_init,
685703
.read_status = tja11xx_read_status,
704+
.get_sqi = tja11xx_get_sqi,
705+
.get_sqi_max = tja11xx_get_sqi_max,
686706
.suspend = genphy_suspend,
687707
.resume = genphy_resume,
688708
.set_loopback = genphy_loopback,
@@ -699,6 +719,8 @@ static struct phy_driver tja11xx_driver[] = {
699719
.config_aneg = tja11xx_config_aneg,
700720
.config_init = tja11xx_config_init,
701721
.read_status = tja11xx_read_status,
722+
.get_sqi = tja11xx_get_sqi,
723+
.get_sqi_max = tja11xx_get_sqi_max,
702724
.suspend = genphy_suspend,
703725
.resume = genphy_resume,
704726
.set_loopback = genphy_loopback,
@@ -715,6 +737,8 @@ static struct phy_driver tja11xx_driver[] = {
715737
.config_aneg = tja11xx_config_aneg,
716738
.config_init = tja11xx_config_init,
717739
.read_status = tja11xx_read_status,
740+
.get_sqi = tja11xx_get_sqi,
741+
.get_sqi_max = tja11xx_get_sqi_max,
718742
.match_phy_device = tja1102_p0_match_phy_device,
719743
.suspend = genphy_suspend,
720744
.resume = genphy_resume,
@@ -736,6 +760,8 @@ static struct phy_driver tja11xx_driver[] = {
736760
.config_aneg = tja11xx_config_aneg,
737761
.config_init = tja11xx_config_init,
738762
.read_status = tja11xx_read_status,
763+
.get_sqi = tja11xx_get_sqi,
764+
.get_sqi_max = tja11xx_get_sqi_max,
739765
.match_phy_device = tja1102_p1_match_phy_device,
740766
.suspend = genphy_suspend,
741767
.resume = genphy_resume,

include/linux/phy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ struct phy_driver {
723723
struct ethtool_tunable *tuna,
724724
const void *data);
725725
int (*set_loopback)(struct phy_device *dev, bool enable);
726+
int (*get_sqi)(struct phy_device *dev);
727+
int (*get_sqi_max)(struct phy_device *dev);
726728
};
727729
#define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
728730
struct phy_driver, mdiodrv)

include/uapi/linux/ethtool_netlink.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ enum {
232232
ETHTOOL_A_LINKSTATE_UNSPEC,
233233
ETHTOOL_A_LINKSTATE_HEADER, /* nest - _A_HEADER_* */
234234
ETHTOOL_A_LINKSTATE_LINK, /* u8 */
235+
ETHTOOL_A_LINKSTATE_SQI, /* u32 */
236+
ETHTOOL_A_LINKSTATE_SQI_MAX, /* u32 */
235237

236238
/* add new constants above here */
237239
__ETHTOOL_A_LINKSTATE_CNT,

net/ethtool/linkstate.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "netlink.h"
44
#include "common.h"
5+
#include <linux/phy.h>
56

67
struct linkstate_req_info {
78
struct ethnl_req_info base;
@@ -10,6 +11,8 @@ struct linkstate_req_info {
1011
struct linkstate_reply_data {
1112
struct ethnl_reply_data base;
1213
int link;
14+
int sqi;
15+
int sqi_max;
1316
};
1417

1518
#define LINKSTATE_REPDATA(__reply_base) \
@@ -20,8 +23,46 @@ linkstate_get_policy[ETHTOOL_A_LINKSTATE_MAX + 1] = {
2023
[ETHTOOL_A_LINKSTATE_UNSPEC] = { .type = NLA_REJECT },
2124
[ETHTOOL_A_LINKSTATE_HEADER] = { .type = NLA_NESTED },
2225
[ETHTOOL_A_LINKSTATE_LINK] = { .type = NLA_REJECT },
26+
[ETHTOOL_A_LINKSTATE_SQI] = { .type = NLA_REJECT },
27+
[ETHTOOL_A_LINKSTATE_SQI_MAX] = { .type = NLA_REJECT },
2328
};
2429

30+
static int linkstate_get_sqi(struct net_device *dev)
31+
{
32+
struct phy_device *phydev = dev->phydev;
33+
int ret;
34+
35+
if (!phydev)
36+
return -EOPNOTSUPP;
37+
38+
mutex_lock(&phydev->lock);
39+
if (!phydev->drv || !phydev->drv->get_sqi)
40+
ret = -EOPNOTSUPP;
41+
else
42+
ret = phydev->drv->get_sqi(phydev);
43+
mutex_unlock(&phydev->lock);
44+
45+
return ret;
46+
}
47+
48+
static int linkstate_get_sqi_max(struct net_device *dev)
49+
{
50+
struct phy_device *phydev = dev->phydev;
51+
int ret;
52+
53+
if (!phydev)
54+
return -EOPNOTSUPP;
55+
56+
mutex_lock(&phydev->lock);
57+
if (!phydev->drv || !phydev->drv->get_sqi_max)
58+
ret = -EOPNOTSUPP;
59+
else
60+
ret = phydev->drv->get_sqi_max(phydev);
61+
mutex_unlock(&phydev->lock);
62+
63+
return ret;
64+
}
65+
2566
static int linkstate_prepare_data(const struct ethnl_req_info *req_base,
2667
struct ethnl_reply_data *reply_base,
2768
struct genl_info *info)
@@ -34,6 +75,19 @@ static int linkstate_prepare_data(const struct ethnl_req_info *req_base,
3475
if (ret < 0)
3576
return ret;
3677
data->link = __ethtool_get_link(dev);
78+
79+
ret = linkstate_get_sqi(dev);
80+
if (ret < 0 && ret != -EOPNOTSUPP)
81+
return ret;
82+
83+
data->sqi = ret;
84+
85+
ret = linkstate_get_sqi_max(dev);
86+
if (ret < 0 && ret != -EOPNOTSUPP)
87+
return ret;
88+
89+
data->sqi_max = ret;
90+
3791
ethnl_ops_complete(dev);
3892

3993
return 0;
@@ -42,8 +96,19 @@ static int linkstate_prepare_data(const struct ethnl_req_info *req_base,
4296
static int linkstate_reply_size(const struct ethnl_req_info *req_base,
4397
const struct ethnl_reply_data *reply_base)
4498
{
45-
return nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */
99+
struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
100+
int len;
101+
102+
len = nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */
46103
+ 0;
104+
105+
if (data->sqi != -EOPNOTSUPP)
106+
len += nla_total_size(sizeof(u32));
107+
108+
if (data->sqi_max != -EOPNOTSUPP)
109+
len += nla_total_size(sizeof(u32));
110+
111+
return len;
47112
}
48113

49114
static int linkstate_fill_reply(struct sk_buff *skb,
@@ -56,6 +121,14 @@ static int linkstate_fill_reply(struct sk_buff *skb,
56121
nla_put_u8(skb, ETHTOOL_A_LINKSTATE_LINK, !!data->link))
57122
return -EMSGSIZE;
58123

124+
if (data->sqi != -EOPNOTSUPP &&
125+
nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI, data->sqi))
126+
return -EMSGSIZE;
127+
128+
if (data->sqi_max != -EOPNOTSUPP &&
129+
nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI_MAX, data->sqi_max))
130+
return -EMSGSIZE;
131+
59132
return 0;
60133
}
61134

0 commit comments

Comments
 (0)