kernel: sfp: re-attempt probing for phy
[openwrt/openwrt.git] / target / linux / generic / pending-4.19 / 759-net-sfp-re-attempt-probing-for-phy.patch
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
5
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
8 no PHY.
9
10 Tested with:
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
14
15 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
16 ---
17 drivers/net/phy/sfp.c | 59 ++++++++++++++++++++++++++++++++++++---------------
18 1 file changed, 42 insertions(+), 17 deletions(-)
19
20 --- a/drivers/net/phy/sfp.c
21 +++ b/drivers/net/phy/sfp.c
22 @@ -60,6 +60,7 @@ enum {
23 SFP_S_FAIL,
24 SFP_S_WAIT,
25 SFP_S_INIT,
26 + SFP_S_INIT_PHY,
27 SFP_S_INIT_TX_FAULT,
28 SFP_S_WAIT_LOS,
29 SFP_S_LINK_UP,
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
40 #define N_FAULT 5
41
42 +/* T_PHY_RETRY is the time interval between attempts to probe the PHY.
43 + * R_PHY_RETRY is the number of attempts.
44 + */
45 +#define T_PHY_RETRY msecs_to_jiffies(50)
46 +#define R_PHY_RETRY 12
47 +
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;
56
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 *
60 int err;
61
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");
65 - return 0;
66 - }
67 + if (phy == ERR_PTR(-ENODEV))
68 + return PTR_ERR(phy);
69 if (IS_ERR(phy)) {
70 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
71 return 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)
74 {
75 unsigned long timeout;
76 + int ret;
77
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.
86 - */
87 - if (sfp_sm_probe_for_phy(sfp)) {
88 - sfp_sm_next(sfp, SFP_S_FAIL, 0);
89 - break;
90 - }
91 - if (sfp_module_start(sfp->sfp_bus)) {
92 - sfp_sm_next(sfp, SFP_S_FAIL, 0);
93 + init_done:
94 + sfp->sm_phy_retries = R_PHY_RETRY;
95 + goto phy_probe;
96 + }
97 + break;
98 +
99 + case SFP_S_INIT_PHY:
100 + if (event != SFP_E_TIMEOUT)
101 + break;
102 + phy_probe:
103 + /* TX_FAULT deasserted or we timed out with TX_FAULT
104 + * clear. Probe for the PHY and check the LOS state.
105 + */
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);
110 break;
111 + } else {
112 + dev_info(sfp->dev, "no PHY detected\n");
113 }
114 - sfp_sm_link_check_los(sfp);
115 -
116 - /* Reset the fault retry count */
117 - sfp->sm_fault_retries = N_FAULT;
118 + } else if (ret) {
119 + sfp_sm_next(sfp, SFP_S_FAIL, 0);
120 + break;
121 }
122 + if (sfp_module_start(sfp->sfp_bus)) {
123 + sfp_sm_next(sfp, SFP_S_FAIL, 0);
124 + break;
125 + }
126 + sfp_sm_link_check_los(sfp);
127 +
128 + /* Reset the fault retry count */
129 + sfp->sm_fault_retries = N_FAULT;
130 break;
131
132 case SFP_S_INIT_TX_FAULT: