1 From ec6036a58f979c66bbd5cd9d0d1c783a98c2c644 Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@armlinux.org.uk>
3 Date: Tue, 5 Nov 2019 12:57:40 +0000
4 Subject: [PATCH 630/660] net: sfp: track upstream's attachment state in state
7 Track the upstream's attachment state in the state machine rather than
8 maintaining a boolean, which ensures that we have a strict order of
9 ATTACH followed by an UP event - we can never believe that a newly
10 attached upstream will be anything but down.
12 Rearrange the order of state machines so we run the module state
13 machine after the upstream device's state machine, so the module state
14 machine can check the current state of the device and take action to
15 e.g. reset back to empty state when the upstream is detached.
17 This is to allow the module detection to run independently of the
18 network device becoming available.
20 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
22 drivers/net/phy/sfp.c | 42 +++++++++++++++++++++++++++++-------------
23 1 file changed, 29 insertions(+), 13 deletions(-)
25 --- a/drivers/net/phy/sfp.c
26 +++ b/drivers/net/phy/sfp.c
27 @@ -34,6 +34,8 @@ enum {
36 @@ -48,7 +50,8 @@ enum {
41 + SFP_DEV_DETACHED = 0,
46 @@ -78,6 +81,7 @@ static const char *mod_state_to_str(unsi
49 static const char * const dev_state_strings[] = {
50 + [SFP_DEV_DETACHED] = "detached",
51 [SFP_DEV_DOWN] = "down",
54 @@ -92,6 +96,8 @@ static const char *dev_state_to_str(unsi
55 static const char * const event_strings[] = {
56 [SFP_E_INSERT] = "insert",
57 [SFP_E_REMOVE] = "remove",
58 + [SFP_E_DEV_ATTACH] = "dev_attach",
59 + [SFP_E_DEV_DETACH] = "dev_detach",
60 [SFP_E_DEV_DOWN] = "dev_down",
61 [SFP_E_DEV_UP] = "dev_up",
62 [SFP_E_TX_FAULT] = "tx_fault",
63 @@ -186,7 +192,6 @@ struct sfp {
64 struct gpio_desc *gpio[GPIO_MAX];
65 int gpio_irq[GPIO_MAX];
68 struct mutex st_mutex; /* Protects state */
70 struct delayed_work poll;
71 @@ -1494,17 +1499,26 @@ static void sfp_sm_mod_remove(struct sfp
72 dev_info(sfp->dev, "module removed\n");
75 -/* This state machine tracks the netdev up/down state */
76 +/* This state machine tracks the upstream's state */
77 static void sfp_sm_device(struct sfp *sfp, unsigned int event)
79 switch (sfp->sm_dev_state) {
81 - if (event == SFP_E_DEV_UP)
82 + if (event == SFP_E_DEV_ATTACH)
83 + sfp->sm_dev_state = SFP_DEV_DOWN;
87 + if (event == SFP_E_DEV_DETACH)
88 + sfp->sm_dev_state = SFP_DEV_DETACHED;
89 + else if (event == SFP_E_DEV_UP)
90 sfp->sm_dev_state = SFP_DEV_UP;
94 - if (event == SFP_E_DEV_DOWN)
95 + if (event == SFP_E_DEV_DETACH)
96 + sfp->sm_dev_state = SFP_DEV_DETACHED;
97 + else if (event == SFP_E_DEV_DOWN)
98 sfp->sm_dev_state = SFP_DEV_DOWN;
101 @@ -1515,17 +1529,20 @@ static void sfp_sm_device(struct sfp *sf
103 static void sfp_sm_module(struct sfp *sfp, unsigned int event)
105 - /* Handle remove event globally, it resets this state machine */
106 - if (event == SFP_E_REMOVE) {
107 + /* Handle remove event globally, it resets this state machine.
108 + * Also deal with upstream detachment.
110 + if (event == SFP_E_REMOVE || sfp->sm_dev_state < SFP_DEV_DOWN) {
111 if (sfp->sm_mod_state > SFP_MOD_PROBE)
112 sfp_sm_mod_remove(sfp);
113 - sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
114 + if (sfp->sm_mod_state != SFP_MOD_EMPTY)
115 + sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
119 switch (sfp->sm_mod_state) {
121 - if (event == SFP_E_INSERT && sfp->attached)
122 + if (event == SFP_E_INSERT)
123 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
126 @@ -1691,8 +1708,8 @@ static void sfp_sm_event(struct sfp *sfp
127 sm_state_to_str(sfp->sm_state),
128 event_to_str(event));
130 - sfp_sm_module(sfp, event);
131 sfp_sm_device(sfp, event);
132 + sfp_sm_module(sfp, event);
133 sfp_sm_main(sfp, event);
135 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
136 @@ -1705,15 +1722,14 @@ static void sfp_sm_event(struct sfp *sfp
138 static void sfp_attach(struct sfp *sfp)
140 - sfp->attached = true;
141 + sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
142 if (sfp->state & SFP_F_PRESENT)
143 sfp_sm_event(sfp, SFP_E_INSERT);
146 static void sfp_detach(struct sfp *sfp)
148 - sfp->attached = false;
149 - sfp_sm_event(sfp, SFP_E_REMOVE);
150 + sfp_sm_event(sfp, SFP_E_DEV_DETACH);
153 static void sfp_start(struct sfp *sfp)