1 From: Florian Fainelli <f.fainelli@gmail.com>
2 Date: Tue, 29 Nov 2016 09:57:17 -0800
3 Subject: [PATCH] net: phy: broadcom: Add support code for reading PHY counters
5 Broadcom PHYs expose a number of PHY error counters: receive errors,
6 false carrier sense, SerDes BER count, local and remote receive errors.
7 Add support code to allow retrieving these error counters. Since the
8 Broadcom PHY library code is used by several drivers, make it possible
9 for them to specify the storage for the software copy of the statistics.
11 Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
12 Signed-off-by: David S. Miller <davem@davemloft.net>
15 --- a/drivers/net/phy/bcm-phy-lib.c
16 +++ b/drivers/net/phy/bcm-phy-lib.c
18 #include <linux/mdio.h>
19 #include <linux/module.h>
20 #include <linux/phy.h>
21 +#include <linux/ethtool.h>
23 #define MII_BCM_CHANNEL_WIDTH 0x2000
24 #define BCM_CL45VEN_EEE_ADV 0x3c
25 @@ -231,6 +232,75 @@ int bcm_phy_set_eee(struct phy_device *p
27 EXPORT_SYMBOL_GPL(bcm_phy_set_eee);
29 +struct bcm_phy_hw_stat {
36 +/* Counters freeze at either 0xffff or 0xff, better than nothing */
37 +static const struct bcm_phy_hw_stat bcm_phy_hw_stats[] = {
38 + { "phy_receive_errors", MII_BRCM_CORE_BASE12, 0, 16 },
39 + { "phy_serdes_ber_errors", MII_BRCM_CORE_BASE13, 8, 8 },
40 + { "phy_false_carrier_sense_errors", MII_BRCM_CORE_BASE13, 0, 8 },
41 + { "phy_local_rcvr_nok", MII_BRCM_CORE_BASE14, 8, 8 },
42 + { "phy_remote_rcv_nok", MII_BRCM_CORE_BASE14, 0, 8 },
45 +int bcm_phy_get_sset_count(struct phy_device *phydev)
47 + return ARRAY_SIZE(bcm_phy_hw_stats);
49 +EXPORT_SYMBOL_GPL(bcm_phy_get_sset_count);
51 +void bcm_phy_get_strings(struct phy_device *phydev, u8 *data)
55 + for (i = 0; i < ARRAY_SIZE(bcm_phy_hw_stats); i++)
56 + memcpy(data + i * ETH_GSTRING_LEN,
57 + bcm_phy_hw_stats[i].string, ETH_GSTRING_LEN);
59 +EXPORT_SYMBOL_GPL(bcm_phy_get_strings);
62 +#define UINT64_MAX (u64)(~((u64)0))
65 +/* Caller is supposed to provide appropriate storage for the library code to
66 + * access the shadow copy
68 +static u64 bcm_phy_get_stat(struct phy_device *phydev, u64 *shadow,
71 + struct bcm_phy_hw_stat stat = bcm_phy_hw_stats[i];
75 + val = phy_read(phydev, stat.reg);
80 + val = val & ((1 << stat.bits) - 1);
88 +void bcm_phy_get_stats(struct phy_device *phydev, u64 *shadow,
89 + struct ethtool_stats *stats, u64 *data)
93 + for (i = 0; i < ARRAY_SIZE(bcm_phy_hw_stats); i++)
94 + data[i] = bcm_phy_get_stat(phydev, shadow, i);
96 +EXPORT_SYMBOL_GPL(bcm_phy_get_stats);
98 MODULE_DESCRIPTION("Broadcom PHY Library");
99 MODULE_LICENSE("GPL v2");
100 MODULE_AUTHOR("Broadcom Corporation");
101 --- a/drivers/net/phy/bcm-phy-lib.h
102 +++ b/drivers/net/phy/bcm-phy-lib.h
103 @@ -37,4 +37,10 @@ int bcm_phy_config_intr(struct phy_devic
104 int bcm_phy_enable_apd(struct phy_device *phydev, bool dll_pwr_down);
106 int bcm_phy_set_eee(struct phy_device *phydev, bool enable);
108 +int bcm_phy_get_sset_count(struct phy_device *phydev);
109 +void bcm_phy_get_strings(struct phy_device *phydev, u8 *data);
110 +void bcm_phy_get_stats(struct phy_device *phydev, u64 *shadow,
111 + struct ethtool_stats *stats, u64 *data);
113 #endif /* _LINUX_BCM_PHY_LIB_H */
114 --- a/include/linux/brcmphy.h
115 +++ b/include/linux/brcmphy.h
117 #define LPI_FEATURE_EN_DIG1000X 0x4000
119 /* Core register definitions*/
120 +#define MII_BRCM_CORE_BASE12 0x12
121 +#define MII_BRCM_CORE_BASE13 0x13
122 +#define MII_BRCM_CORE_BASE14 0x14
123 #define MII_BRCM_CORE_BASE1E 0x1E
124 #define MII_BRCM_CORE_EXPB0 0xB0
125 #define MII_BRCM_CORE_EXPB1 0xB1