1 From: Russell King <rmk+kernel@arm.linux.org.uk>
2 Date: Thu, 1 Oct 2015 20:32:07 +0100
3 Subject: [PATCH] phylink: add flow control support
5 Add flow control support, including ethtool support, to phylink. We
6 add support to allow ethtool to get and set the current flow control
7 settings, and the 802.3 specified resolution for the local and remote
8 link partner abilities.
10 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
13 --- a/drivers/net/phy/phylink.c
14 +++ b/drivers/net/phy/phylink.c
15 @@ -132,6 +132,9 @@ static int phylink_parse_fixedlink(struc
17 if (of_property_read_bool(fixed_node, "full-duplex"))
18 pl->link_config.duplex = DUPLEX_FULL;
20 + /* We treat the "pause" and "asym-pause" terminology as
21 + * defining the link partner's ability. */
22 if (of_property_read_bool(fixed_node, "pause"))
23 pl->link_config.pause |= MLO_PAUSE_SYM;
24 if (of_property_read_bool(fixed_node, "asym-pause"))
25 @@ -277,6 +280,56 @@ static void phylink_get_fixed_state(stru
26 state->link = !!gpiod_get_value(pl->link_gpio);
29 +/* Flow control is resolved according to our and the link partners
30 + * advertisments using the following drawn from the 802.3 specs:
31 + * Local device Link partner
32 + * Pause AsymDir Pause AsymDir Result
37 +static void phylink_resolve_flow(struct phylink *pl,
38 + struct phylink_link_state *state)
42 + if (pl->link_config.pause & MLO_PAUSE_AN) {
45 + if (phylink_test(pl->link_config.advertising, Pause))
46 + pause |= MLO_PAUSE_SYM;
47 + if (phylink_test(pl->link_config.advertising, Asym_Pause))
48 + pause |= MLO_PAUSE_ASYM;
50 + pause &= state->pause;
52 + if (pause & MLO_PAUSE_SYM)
53 + new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
54 + else if (pause & MLO_PAUSE_ASYM)
55 + new_pause = state->pause & MLO_PAUSE_SYM ?
56 + MLO_PAUSE_RX : MLO_PAUSE_TX;
58 + new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
61 + state->pause &= ~MLO_PAUSE_TXRX_MASK;
62 + state->pause |= new_pause;
65 +static const char *phylink_pause_to_str(int pause)
67 + switch (pause & MLO_PAUSE_TXRX_MASK) {
68 + case MLO_PAUSE_TX | MLO_PAUSE_RX:
79 static void phylink_resolve(struct work_struct *w)
81 struct phylink *pl = container_of(w, struct phylink, resolve);
82 @@ -290,6 +343,7 @@ static void phylink_resolve(struct work_
83 switch (pl->link_an_mode) {
85 link_state = pl->phy_state;
86 + phylink_resolve_flow(pl, &link_state);
90 @@ -298,9 +352,12 @@ static void phylink_resolve(struct work_
93 phylink_get_mac_state(pl, &link_state);
96 link_state.link = link_state.link &&
98 + link_state.pause |= pl->phy_state.pause;
99 + phylink_resolve_flow(pl, &link_state);
104 @@ -330,7 +387,7 @@ static void phylink_resolve(struct work_
105 "Link is Up - %s/%s - flow control %s\n",
106 phy_speed_to_str(link_state.speed),
107 phy_duplex_to_str(link_state.duplex),
108 - link_state.pause ? "rx/tx" : "off");
109 + phylink_pause_to_str(link_state.pause));
112 mutex_unlock(&pl->state_mutex);
113 @@ -358,6 +415,7 @@ struct phylink *phylink_create(struct ne
115 pl->link_interface = iface;
116 pl->link_port = PORT_MII;
117 + pl->link_config.pause = MLO_PAUSE_AN;
118 pl->link_config.speed = SPEED_UNKNOWN;
119 pl->link_config.duplex = DUPLEX_UNKNOWN;
121 @@ -580,6 +638,7 @@ void phylink_start(struct phylink *pl)
122 * a fixed-link to start with the correct parameters, and also
123 * ensures that we set the appropriate advertisment for Serdes links.
125 + phylink_resolve_flow(pl, &pl->link_config);
126 phylink_mac_config(pl, &pl->link_config);
128 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
129 @@ -797,6 +856,79 @@ int phylink_ethtool_nway_reset(struct ph
131 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
133 +void phylink_ethtool_get_pauseparam(struct phylink *pl,
134 + struct ethtool_pauseparam *pause)
136 + mutex_lock(&pl->config_mutex);
138 + pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
139 + pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
140 + pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
142 + mutex_unlock(&pl->config_mutex);
144 +EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
146 +static int __phylink_ethtool_set_pauseparam(struct phylink *pl,
147 + struct ethtool_pauseparam *pause)
149 + struct phylink_link_state *config = &pl->link_config;
151 + if (!phylink_test(pl->supported, Pause) &&
152 + !phylink_test(pl->supported, Asym_Pause))
153 + return -EOPNOTSUPP;
155 + if (!phylink_test(pl->supported, Asym_Pause) &&
156 + !pause->autoneg && pause->rx_pause != pause->tx_pause)
159 + config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
161 + if (pause->autoneg)
162 + config->pause |= MLO_PAUSE_AN;
163 + if (pause->rx_pause)
164 + config->pause |= MLO_PAUSE_RX;
165 + if (pause->tx_pause)
166 + config->pause |= MLO_PAUSE_TX;
168 + if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
169 + switch (pl->link_an_mode) {
171 + /* Silently mark the carrier down, and then trigger a resolve */
172 + netif_carrier_off(pl->netdev);
173 + phylink_run_resolve(pl);
177 + /* Should we allow fixed links to change against the config? */
178 + phylink_resolve_flow(pl, config);
179 + phylink_mac_config(pl, config);
184 + phylink_mac_config(pl, config);
185 + phylink_mac_an_restart(pl);
193 +int phylink_ethtool_set_pauseparam(struct phylink *pl,
194 + struct ethtool_pauseparam *pause)
198 + mutex_lock(&pl->config_mutex);
199 + ret = __phylink_ethtool_set_pauseparam(pl, pause);
200 + mutex_unlock(&pl->config_mutex);
204 +EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
206 /* This emulates MII registers for a fixed-mode phy operating as per the
207 * passed in state. "aneg" defines if we report negotiation is possible.
209 --- a/include/linux/phylink.h
210 +++ b/include/linux/phylink.h
211 @@ -13,6 +13,10 @@ enum {
213 MLO_PAUSE_ASYM = BIT(0),
214 MLO_PAUSE_SYM = BIT(1),
215 + MLO_PAUSE_RX = BIT(2),
216 + MLO_PAUSE_TX = BIT(3),
217 + MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX,
218 + MLO_PAUSE_AN = BIT(4),
222 @@ -87,6 +91,10 @@ int phylink_ethtool_ksettings_get(struct
223 int phylink_ethtool_ksettings_set(struct phylink *,
224 const struct ethtool_link_ksettings *);
225 int phylink_ethtool_nway_reset(struct phylink *);
226 +void phylink_ethtool_get_pauseparam(struct phylink *,
227 + struct ethtool_pauseparam *);
228 +int phylink_ethtool_set_pauseparam(struct phylink *,
229 + struct ethtool_pauseparam *);
230 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
232 int phylink_set_link(struct phylink *pl, unsigned int mode, u8 port,