1 From d8b4e728f598d3c8a9b219d4679d5de350caa082 Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@arm.linux.org.uk>
3 Date: Fri, 18 Sep 2015 14:42:16 +0100
4 Subject: [PATCH 714/744] phy: provide a hook for link up/link down events
6 Sometimes, we need to do additional work between the PHY coming up and
7 marking the carrier present - for example, we may need to wait for the
8 PHY to MAC link to finish negotiation. This changes phylib to provide
9 a notification function pointer which avoids the built-in
10 netif_carrier_on() and netif_carrier_off() functions.
12 Standard ->adjust_link functionality is provided by hooking a helper
13 into the new ->phy_link_change method.
15 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
16 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
18 drivers/net/phy/phy.c | 42 ++++++++++++++++++++++--------------------
19 drivers/net/phy/phy_device.c | 14 ++++++++++++++
20 include/linux/phy.h | 1 +
21 3 files changed, 37 insertions(+), 20 deletions(-)
23 --- a/drivers/net/phy/phy.c
24 +++ b/drivers/net/phy/phy.c
25 @@ -849,6 +849,16 @@ void phy_start(struct phy_device *phydev
27 EXPORT_SYMBOL(phy_start);
29 +static void phy_link_up(struct phy_device *phydev)
31 + phydev->phy_link_change(phydev, true, true);
34 +static void phy_link_down(struct phy_device *phydev, bool do_carrier)
36 + phydev->phy_link_change(phydev, false, do_carrier);
40 * phy_state_machine - Handle the state machine
41 * @work: work_struct that describes the work to be done
42 @@ -890,8 +900,7 @@ void phy_state_machine(struct work_struc
43 /* If the link is down, give up on negotiation for now */
45 phydev->state = PHY_NOLINK;
46 - netif_carrier_off(phydev->attached_dev);
47 - phydev->adjust_link(phydev->attached_dev);
48 + phy_link_down(phydev, true);
52 @@ -903,9 +912,7 @@ void phy_state_machine(struct work_struc
53 /* If AN is done, we're running */
55 phydev->state = PHY_RUNNING;
56 - netif_carrier_on(phydev->attached_dev);
57 - phydev->adjust_link(phydev->attached_dev);
59 + phy_link_up(phydev);
60 } else if (0 == phydev->link_timeout--)
63 @@ -930,8 +937,7 @@ void phy_state_machine(struct work_struc
66 phydev->state = PHY_RUNNING;
67 - netif_carrier_on(phydev->attached_dev);
68 - phydev->adjust_link(phydev->attached_dev);
69 + phy_link_up(phydev);
73 @@ -941,13 +947,12 @@ void phy_state_machine(struct work_struc
76 phydev->state = PHY_RUNNING;
77 - netif_carrier_on(phydev->attached_dev);
78 + phy_link_up(phydev);
80 if (0 == phydev->link_timeout--)
82 + phy_link_down(phydev, false);
85 - phydev->adjust_link(phydev->attached_dev);
88 /* Only register a CHANGE if we are polling or ignoring
89 @@ -970,14 +975,12 @@ void phy_state_machine(struct work_struc
92 phydev->state = PHY_RUNNING;
93 - netif_carrier_on(phydev->attached_dev);
94 + phy_link_up(phydev);
96 phydev->state = PHY_NOLINK;
97 - netif_carrier_off(phydev->attached_dev);
98 + phy_link_down(phydev, true);
101 - phydev->adjust_link(phydev->attached_dev);
103 if (phy_interrupt_is_valid(phydev))
104 err = phy_config_interrupt(phydev,
105 PHY_INTERRUPT_ENABLED);
106 @@ -985,8 +988,7 @@ void phy_state_machine(struct work_struc
110 - netif_carrier_off(phydev->attached_dev);
111 - phydev->adjust_link(phydev->attached_dev);
112 + phy_link_down(phydev, true);
116 @@ -1006,11 +1008,11 @@ void phy_state_machine(struct work_struc
119 phydev->state = PHY_RUNNING;
120 - netif_carrier_on(phydev->attached_dev);
121 + phy_link_up(phydev);
123 phydev->state = PHY_NOLINK;
124 + phy_link_down(phydev, false);
126 - phydev->adjust_link(phydev->attached_dev);
128 phydev->state = PHY_AN;
129 phydev->link_timeout = PHY_AN_TIMEOUT;
130 @@ -1022,11 +1024,11 @@ void phy_state_machine(struct work_struc
133 phydev->state = PHY_RUNNING;
134 - netif_carrier_on(phydev->attached_dev);
135 + phy_link_up(phydev);
137 phydev->state = PHY_NOLINK;
138 + phy_link_down(phydev, false);
140 - phydev->adjust_link(phydev->attached_dev);
144 --- a/drivers/net/phy/phy_device.c
145 +++ b/drivers/net/phy/phy_device.c
146 @@ -441,6 +441,19 @@ struct phy_device *phy_find_first(struct
148 EXPORT_SYMBOL(phy_find_first);
150 +static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
152 + struct net_device *netdev = phydev->attached_dev;
156 + netif_carrier_on(netdev);
158 + netif_carrier_off(netdev);
160 + phydev->adjust_link(netdev);
164 * phy_prepare_link - prepares the PHY layer to monitor link status
165 * @phydev: target phy_device struct
166 @@ -659,6 +672,7 @@ int phy_attach_direct(struct net_device
170 + phydev->phy_link_change = phy_link_change;
171 phydev->attached_dev = dev;
172 dev->phydev = phydev;
174 --- a/include/linux/phy.h
175 +++ b/include/linux/phy.h
176 @@ -433,6 +433,7 @@ struct phy_device {
180 + void (*phy_link_change)(struct phy_device *, bool up, bool do_carrier);
181 void (*adjust_link)(struct net_device *dev);
183 #define to_phy_device(d) container_of(d, struct phy_device, dev)