kernel: update 4.4 to 4.4.83
[openwrt/openwrt.git] / target / linux / mvebu / patches-4.4 / 138-phylink-add-flow-control-support.patch
1 From f566177aa6661e646b83526f24391a568ffd1a75 Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@arm.linux.org.uk>
3 Date: Thu, 1 Oct 2015 20:32:07 +0100
4 Subject: [PATCH 726/744] phylink: add flow control support
5
6 Add flow control support, including ethtool support, to phylink. We
7 add support to allow ethtool to get and set the current flow control
8 settings, and the 802.3 specified resolution for the local and remote
9 link partner abilities.
10
11 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
12 ---
13 drivers/net/phy/phylink.c | 145 +++++++++++++++++++++++++++++++++++++++++-----
14 include/linux/phylink.h | 8 +++
15 2 files changed, 139 insertions(+), 14 deletions(-)
16
17 --- a/drivers/net/phy/phylink.c
18 +++ b/drivers/net/phy/phylink.c
19 @@ -91,10 +91,12 @@ static int phylink_parse_fixedlink(struc
20 pl->link_config.an_complete = 1;
21 pl->link_config.speed = speed;
22 pl->link_config.duplex = DUPLEX_HALF;
23 - pl->link_config.pause = MLO_PAUSE_NONE;
24
25 if (of_property_read_bool(fixed_node, "full-duplex"))
26 pl->link_config.duplex = DUPLEX_FULL;
27 +
28 + /* We treat the "pause" and "asym-pause" terminology as
29 + * defining the link partner's ability. */
30 if (of_property_read_bool(fixed_node, "pause"))
31 pl->link_config.pause |= MLO_PAUSE_SYM;
32 if (of_property_read_bool(fixed_node, "asym-pause"))
33 @@ -118,7 +120,6 @@ static int phylink_parse_fixedlink(struc
34 pl->link_config.duplex = be32_to_cpu(fixed_prop[1]) ?
35 DUPLEX_FULL : DUPLEX_HALF;
36 pl->link_config.speed = be32_to_cpu(fixed_prop[2]);
37 - pl->link_config.pause = MLO_PAUSE_NONE;
38 if (be32_to_cpu(fixed_prop[3]))
39 pl->link_config.pause |= MLO_PAUSE_SYM;
40 if (be32_to_cpu(fixed_prop[4]))
41 @@ -130,16 +131,6 @@ static int phylink_parse_fixedlink(struc
42 }
43
44 if (pl->link_an_mode == MLO_AN_FIXED) {
45 - /* Generate the supported/advertising masks */
46 - if (pl->link_config.pause & MLO_PAUSE_SYM) {
47 - pl->link_config.supported |= SUPPORTED_Pause;
48 - pl->link_config.advertising |= ADVERTISED_Pause;
49 - }
50 - if (pl->link_config.pause & MLO_PAUSE_ASYM) {
51 - pl->link_config.supported |= SUPPORTED_Asym_Pause;
52 - pl->link_config.advertising |= ADVERTISED_Asym_Pause;
53 - }
54 -
55 if (pl->link_config.speed > SPEED_1000 &&
56 pl->link_config.duplex != DUPLEX_FULL)
57 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
58 @@ -242,6 +233,56 @@ static void phylink_get_fixed_state(stru
59 state->link = !!gpiod_get_value(pl->link_gpio);
60 }
61
62 +/* Flow control is resolved according to our and the link partners
63 + * advertisments using the following drawn from the 802.3 specs:
64 + * Local device Link partner
65 + * Pause AsymDir Pause AsymDir Result
66 + * 1 X 1 X TX+RX
67 + * 0 1 1 1 RX
68 + * 1 1 0 1 TX
69 + */
70 +static void phylink_resolve_flow(struct phylink *pl,
71 + struct phylink_link_state *state)
72 +{
73 + int new_pause = 0;
74 +
75 + if (pl->link_config.pause & MLO_PAUSE_AN) {
76 + int pause = 0;
77 +
78 + if (pl->link_config.advertising & ADVERTISED_Pause)
79 + pause |= MLO_PAUSE_SYM;
80 + if (pl->link_config.advertising & ADVERTISED_Asym_Pause)
81 + pause |= MLO_PAUSE_ASYM;
82 +
83 + pause &= state->pause;
84 +
85 + if (pause & MLO_PAUSE_SYM)
86 + new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
87 + else if (pause & MLO_PAUSE_ASYM)
88 + new_pause = state->pause & MLO_PAUSE_SYM ?
89 + MLO_PAUSE_RX : MLO_PAUSE_TX;
90 + } else {
91 + new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
92 + }
93 +
94 + state->pause &= ~MLO_PAUSE_TXRX_MASK;
95 + state->pause |= new_pause;
96 +}
97 +
98 +static const char *phylink_pause_to_str(int pause)
99 +{
100 + switch (pause & MLO_PAUSE_TXRX_MASK) {
101 + case MLO_PAUSE_TX | MLO_PAUSE_RX:
102 + return "rx/tx";
103 + case MLO_PAUSE_TX:
104 + return "tx";
105 + case MLO_PAUSE_RX:
106 + return "rx";
107 + default:
108 + return "off";
109 + }
110 +}
111 +
112 extern const char *phy_speed_to_str(int speed);
113
114 static void phylink_resolve(struct work_struct *w)
115 @@ -257,6 +298,7 @@ static void phylink_resolve(struct work_
116 switch (pl->link_an_mode) {
117 case MLO_AN_PHY:
118 link_state = pl->phy_state;
119 + phylink_resolve_flow(pl, &link_state);
120 break;
121
122 case MLO_AN_FIXED:
123 @@ -265,9 +307,12 @@ static void phylink_resolve(struct work_
124
125 case MLO_AN_SGMII:
126 phylink_get_mac_state(pl, &link_state);
127 - if (pl->phydev)
128 + if (pl->phydev) {
129 link_state.link = link_state.link &&
130 pl->phy_state.link;
131 + link_state.pause |= pl->phy_state.pause;
132 + phylink_resolve_flow(pl, &link_state);
133 + }
134 break;
135
136 case MLO_AN_8023Z:
137 @@ -297,7 +342,7 @@ static void phylink_resolve(struct work_
138 "Link is Up - %s/%s - flow control %s\n",
139 phy_speed_to_str(link_state.speed),
140 link_state.duplex ? "Full" : "Half",
141 - link_state.pause ? "rx/tx" : "off");
142 + phylink_pause_to_str(link_state.pause));
143 }
144 }
145 mutex_unlock(&pl->state_mutex);
146 @@ -326,6 +371,7 @@ struct phylink *phylink_create(struct ne
147 pl->link_interface = iface;
148 pl->link_port_support = SUPPORTED_MII;
149 pl->link_port = PORT_MII;
150 + pl->link_config.pause = MLO_PAUSE_AN;
151 pl->ops = ops;
152 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
153
154 @@ -507,6 +553,7 @@ void phylink_start(struct phylink *pl)
155 * a fixed-link to start with the correct parameters, and also
156 * ensures that we set the appropriate advertisment for Serdes links.
157 */
158 + phylink_resolve_flow(pl, &pl->link_config);
159 phylink_mac_config(pl, &pl->link_config);
160
161 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
162 @@ -701,6 +748,76 @@ int phylink_ethtool_nway_reset(struct ph
163 }
164 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
165
166 +void phylink_ethtool_get_pauseparam(struct phylink *pl,
167 + struct ethtool_pauseparam *pause)
168 +{
169 + mutex_lock(&pl->config_mutex);
170 +
171 + pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
172 + pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
173 + pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
174 +
175 + mutex_unlock(&pl->config_mutex);
176 +}
177 +EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
178 +
179 +static int __phylink_ethtool_set_pauseparam(struct phylink *pl,
180 + struct ethtool_pauseparam *pause)
181 +{
182 + struct phylink_link_state *config = &pl->link_config;
183 +
184 + if (!(config->supported & (SUPPORTED_Pause | SUPPORTED_Asym_Pause)))
185 + return -EOPNOTSUPP;
186 +
187 + if (!(config->supported & SUPPORTED_Asym_Pause) &&
188 + !pause->autoneg && pause->rx_pause != pause->tx_pause)
189 + return -EINVAL;
190 +
191 + config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
192 +
193 + if (pause->autoneg)
194 + config->pause |= MLO_PAUSE_AN;
195 + if (pause->rx_pause)
196 + config->pause |= MLO_PAUSE_RX;
197 + if (pause->tx_pause)
198 + config->pause |= MLO_PAUSE_TX;
199 +
200 + switch (pl->link_an_mode) {
201 + case MLO_AN_PHY:
202 + /* Silently mark the carrier down, and then trigger a resolve */
203 + netif_carrier_off(pl->netdev);
204 + phylink_run_resolve(pl);
205 + break;
206 +
207 + case MLO_AN_FIXED:
208 + /* Should we allow fixed links to change against the config? */
209 + phylink_resolve_flow(pl, config);
210 + phylink_mac_config(pl, config);
211 + break;
212 +
213 + case MLO_AN_SGMII:
214 + case MLO_AN_8023Z:
215 + phylink_mac_config(pl, config);
216 + phylink_mac_an_restart(pl);
217 + break;
218 + }
219 +
220 + return 0;
221 +}
222 +
223 +int phylink_ethtool_set_pauseparam(struct phylink *pl,
224 + struct ethtool_pauseparam *pause)
225 +{
226 + int ret;
227 +
228 + mutex_lock(&pl->config_mutex);
229 + ret = __phylink_ethtool_set_pauseparam(pl, pause);
230 + mutex_unlock(&pl->config_mutex);
231 +
232 + return ret;
233 +}
234 +EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
235 +
236 /* This emulates MII registers for a fixed-mode phy operating as per the
237 * passed in state. "aneg" defines if we report negotiation is possible.
238 *
239 --- a/include/linux/phylink.h
240 +++ b/include/linux/phylink.h
241 @@ -13,6 +13,10 @@ enum {
242 MLO_PAUSE_NONE,
243 MLO_PAUSE_ASYM = BIT(0),
244 MLO_PAUSE_SYM = BIT(1),
245 + MLO_PAUSE_RX = BIT(2),
246 + MLO_PAUSE_TX = BIT(3),
247 + MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX,
248 + MLO_PAUSE_AN = BIT(4),
249
250 MLO_AN_PHY = 0,
251 MLO_AN_FIXED,
252 @@ -66,6 +70,10 @@ void phylink_stop(struct phylink *);
253 int phylink_ethtool_get_settings(struct phylink *, struct ethtool_cmd *);
254 int phylink_ethtool_set_settings(struct phylink *, struct ethtool_cmd *);
255 int phylink_ethtool_nway_reset(struct phylink *);
256 +void phylink_ethtool_get_pauseparam(struct phylink *,
257 + struct ethtool_pauseparam *);
258 +int phylink_ethtool_set_pauseparam(struct phylink *,
259 + struct ethtool_pauseparam *);
260 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
261
262 void phylink_set_link_port(struct phylink *pl, u32 support, u8 port);