1 From: Russell King <rmk+kernel@arm.linux.org.uk>
2 Date: Sun, 12 Feb 2017 17:27:17 +0100
3 Subject: [PATCH] phy: provide a hook for link up/link down events
5 Sometimes, we need to do additional work between the PHY coming up and
6 marking the carrier present - for example, we may need to wait for the
7 PHY to MAC link to finish negotiation. This changes phylib to provide
8 a notification function pointer which avoids the built-in
9 netif_carrier_on() and netif_carrier_off() functions.
11 Standard ->adjust_link functionality is provided by hooking a helper
12 into the new ->phy_link_change method.
14 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
17 --- a/drivers/net/phy/phy.c
18 +++ b/drivers/net/phy/phy.c
19 @@ -991,6 +991,16 @@ void phy_start(struct phy_device *phydev
21 EXPORT_SYMBOL(phy_start);
23 +static void phy_link_up(struct phy_device *phydev)
25 + phydev->phy_link_change(phydev, true, true);
28 +static void phy_link_down(struct phy_device *phydev, bool do_carrier)
30 + phydev->phy_link_change(phydev, false, do_carrier);
34 * phy_state_machine - Handle the state machine
35 * @work: work_struct that describes the work to be done
36 @@ -1032,8 +1042,7 @@ void phy_state_machine(struct work_struc
37 /* If the link is down, give up on negotiation for now */
39 phydev->state = PHY_NOLINK;
40 - netif_carrier_off(phydev->attached_dev);
41 - phydev->adjust_link(phydev->attached_dev);
42 + phy_link_down(phydev, true);
46 @@ -1045,9 +1054,7 @@ void phy_state_machine(struct work_struc
47 /* If AN is done, we're running */
49 phydev->state = PHY_RUNNING;
50 - netif_carrier_on(phydev->attached_dev);
51 - phydev->adjust_link(phydev->attached_dev);
53 + phy_link_up(phydev);
54 } else if (0 == phydev->link_timeout--)
57 @@ -1072,8 +1079,7 @@ void phy_state_machine(struct work_struc
60 phydev->state = PHY_RUNNING;
61 - netif_carrier_on(phydev->attached_dev);
62 - phydev->adjust_link(phydev->attached_dev);
63 + phy_link_up(phydev);
67 @@ -1083,13 +1089,12 @@ void phy_state_machine(struct work_struc
70 phydev->state = PHY_RUNNING;
71 - netif_carrier_on(phydev->attached_dev);
72 + phy_link_up(phydev);
74 if (0 == phydev->link_timeout--)
76 + phy_link_down(phydev, false);
79 - phydev->adjust_link(phydev->attached_dev);
82 /* Only register a CHANGE if we are polling and link changed
83 @@ -1112,14 +1117,12 @@ void phy_state_machine(struct work_struc
86 phydev->state = PHY_RUNNING;
87 - netif_carrier_on(phydev->attached_dev);
88 + phy_link_up(phydev);
90 phydev->state = PHY_NOLINK;
91 - netif_carrier_off(phydev->attached_dev);
92 + phy_link_down(phydev, true);
95 - phydev->adjust_link(phydev->attached_dev);
97 if (phy_interrupt_is_valid(phydev))
98 err = phy_config_interrupt(phydev,
99 PHY_INTERRUPT_ENABLED);
100 @@ -1127,8 +1130,7 @@ void phy_state_machine(struct work_struc
104 - netif_carrier_off(phydev->attached_dev);
105 - phydev->adjust_link(phydev->attached_dev);
106 + phy_link_down(phydev, true);
110 @@ -1148,11 +1150,11 @@ void phy_state_machine(struct work_struc
113 phydev->state = PHY_RUNNING;
114 - netif_carrier_on(phydev->attached_dev);
115 + phy_link_up(phydev);
117 phydev->state = PHY_NOLINK;
118 + phy_link_down(phydev, false);
120 - phydev->adjust_link(phydev->attached_dev);
122 phydev->state = PHY_AN;
123 phydev->link_timeout = PHY_AN_TIMEOUT;
124 @@ -1164,11 +1166,11 @@ void phy_state_machine(struct work_struc
127 phydev->state = PHY_RUNNING;
128 - netif_carrier_on(phydev->attached_dev);
129 + phy_link_up(phydev);
131 phydev->state = PHY_NOLINK;
132 + phy_link_down(phydev, false);
134 - phydev->adjust_link(phydev->attached_dev);
138 --- a/drivers/net/phy/phy_device.c
139 +++ b/drivers/net/phy/phy_device.c
140 @@ -645,6 +645,19 @@ struct phy_device *phy_find_first(struct
142 EXPORT_SYMBOL(phy_find_first);
144 +static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
146 + struct net_device *netdev = phydev->attached_dev;
150 + netif_carrier_on(netdev);
152 + netif_carrier_off(netdev);
154 + phydev->adjust_link(netdev);
158 * phy_prepare_link - prepares the PHY layer to monitor link status
159 * @phydev: target phy_device struct
160 @@ -899,6 +912,7 @@ int phy_attach_direct(struct net_device
164 + phydev->phy_link_change = phy_link_change;
165 phydev->attached_dev = dev;
166 dev->phydev = phydev;
168 --- a/include/linux/phy.h
169 +++ b/include/linux/phy.h
170 @@ -426,6 +426,7 @@ struct phy_device {
174 + void (*phy_link_change)(struct phy_device *, bool up, bool do_carrier);
175 void (*adjust_link)(struct net_device *dev);
177 #define to_phy_device(d) container_of(to_mdio_device(d), \