1 From 6c4efe83a0acf6f06c89ae17b885fa5739eb5be7 Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@armlinux.org.uk>
3 Date: Mon, 2 Dec 2019 18:20:22 +0000
4 Subject: [PATCH 4/4] net: sfp: re-attempt probing for phy
6 Some 1000BASE-T PHY modules take a while for the PHY to wake up.
7 Retry the probe a number of times before deciding that the module has
11 Sourcephotonics SPGBTXCNFC - PHY takes less than 50ms to respond.
12 Champion One 1000SFPT - PHY takes about 200ms to respond.
13 Mikrotik S-RJ01 - no PHY
15 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
17 drivers/net/phy/sfp.c | 59 ++++++++++++++++++++++++++++++++++++---------------
18 1 file changed, 42 insertions(+), 17 deletions(-)
20 --- a/drivers/net/phy/sfp.c
21 +++ b/drivers/net/phy/sfp.c
22 @@ -60,6 +60,7 @@ enum {
30 @@ -124,6 +125,7 @@ static const char * const sm_state_strin
31 [SFP_S_FAIL] = "fail",
32 [SFP_S_WAIT] = "wait",
33 [SFP_S_INIT] = "init",
34 + [SFP_S_INIT_PHY] = "init_phy",
35 [SFP_S_INIT_TX_FAULT] = "init_tx_fault",
36 [SFP_S_WAIT_LOS] = "wait_los",
37 [SFP_S_LINK_UP] = "link_up",
38 @@ -168,6 +170,12 @@ static const enum gpiod_flags gpio_flags
39 #define N_FAULT_INIT 5
42 +/* T_PHY_RETRY is the time interval between attempts to probe the PHY.
43 + * R_PHY_RETRY is the number of attempts.
45 +#define T_PHY_RETRY msecs_to_jiffies(50)
46 +#define R_PHY_RETRY 12
48 /* SFP module presence detection is poor: the three MOD DEF signals are
49 * the same length on the PCB, which means it's possible for MOD DEF 0 to
50 * connect before the I2C bus on MOD DEF 1/2.
51 @@ -223,6 +231,7 @@ struct sfp {
52 unsigned char sm_dev_state;
53 unsigned short sm_state;
54 unsigned char sm_fault_retries;
55 + unsigned char sm_phy_retries;
57 struct sfp_eeprom_id id;
58 unsigned int module_power_mW;
59 @@ -1340,10 +1349,8 @@ static int sfp_sm_probe_phy(struct sfp *
62 phy = get_phy_device(sfp->i2c_mii, SFP_PHY_ADDR, is_c45);
63 - if (phy == ERR_PTR(-ENODEV)) {
64 - dev_info(sfp->dev, "no PHY detected\n");
67 + if (phy == ERR_PTR(-ENODEV))
68 + return PTR_ERR(phy);
70 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
72 @@ -1785,6 +1792,7 @@ static void sfp_sm_module(struct sfp *sf
73 static void sfp_sm_main(struct sfp *sfp, unsigned int event)
75 unsigned long timeout;
78 /* Some events are global */
79 if (sfp->sm_state != SFP_S_DOWN &&
80 @@ -1857,22 +1865,39 @@ static void sfp_sm_main(struct sfp *sfp,
81 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
82 sfp->sm_fault_retries == N_FAULT_INIT);
83 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
84 - init_done: /* TX_FAULT deasserted or we timed out with TX_FAULT
85 - * clear. Probe for the PHY and check the LOS state.
87 - if (sfp_sm_probe_for_phy(sfp)) {
88 - sfp_sm_next(sfp, SFP_S_FAIL, 0);
91 - if (sfp_module_start(sfp->sfp_bus)) {
92 - sfp_sm_next(sfp, SFP_S_FAIL, 0);
94 + sfp->sm_phy_retries = R_PHY_RETRY;
99 + case SFP_S_INIT_PHY:
100 + if (event != SFP_E_TIMEOUT)
103 + /* TX_FAULT deasserted or we timed out with TX_FAULT
104 + * clear. Probe for the PHY and check the LOS state.
106 + ret = sfp_sm_probe_for_phy(sfp);
107 + if (ret == -ENODEV) {
108 + if (--sfp->sm_phy_retries) {
109 + sfp_sm_next(sfp, SFP_S_INIT_PHY, T_PHY_RETRY);
112 + dev_info(sfp->dev, "no PHY detected\n");
114 - sfp_sm_link_check_los(sfp);
116 - /* Reset the fault retry count */
117 - sfp->sm_fault_retries = N_FAULT;
119 + sfp_sm_next(sfp, SFP_S_FAIL, 0);
122 + if (sfp_module_start(sfp->sfp_bus)) {
123 + sfp_sm_next(sfp, SFP_S_FAIL, 0);
126 + sfp_sm_link_check_los(sfp);
128 + /* Reset the fault retry count */
129 + sfp->sm_fault_retries = N_FAULT;
132 case SFP_S_INIT_TX_FAULT: