summaryrefslogtreecommitdiff
path: root/target/linux/mvebu/patches-4.4/138-phylink-add-flow-control-support.patch
blob: 95b8a81943a53dd8badb3c8cb60fa08b10a5ab36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
From f566177aa6661e646b83526f24391a568ffd1a75 Mon Sep 17 00:00:00 2001
From: Russell King <rmk+kernel@arm.linux.org.uk>
Date: Thu, 1 Oct 2015 20:32:07 +0100
Subject: [PATCH 726/744] phylink: add flow control support

Add flow control support, including ethtool support, to phylink.  We
add support to allow ethtool to get and set the current flow control
settings, and the 802.3 specified resolution for the local and remote
link partner abilities.

Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
---
 drivers/net/phy/phylink.c | 145 +++++++++++++++++++++++++++++++++++++++++-----
 include/linux/phylink.h   |   8 +++
 2 files changed, 139 insertions(+), 14 deletions(-)

--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -91,10 +91,12 @@ static int phylink_parse_fixedlink(struc
 		pl->link_config.an_complete = 1;
 		pl->link_config.speed = speed;
 		pl->link_config.duplex = DUPLEX_HALF;
-		pl->link_config.pause = MLO_PAUSE_NONE;
 
 		if (of_property_read_bool(fixed_node, "full-duplex"))
 			pl->link_config.duplex = DUPLEX_FULL;
+
+		/* We treat the "pause" and "asym-pause" terminology as
+		 * defining the link partner's ability. */
 		if (of_property_read_bool(fixed_node, "pause"))
 			pl->link_config.pause |= MLO_PAUSE_SYM;
 		if (of_property_read_bool(fixed_node, "asym-pause"))
@@ -118,7 +120,6 @@ static int phylink_parse_fixedlink(struc
 			pl->link_config.duplex = be32_to_cpu(fixed_prop[1]) ?
 						DUPLEX_FULL : DUPLEX_HALF;
 			pl->link_config.speed = be32_to_cpu(fixed_prop[2]);
-			pl->link_config.pause = MLO_PAUSE_NONE;
 			if (be32_to_cpu(fixed_prop[3]))
 				pl->link_config.pause |= MLO_PAUSE_SYM;
 			if (be32_to_cpu(fixed_prop[4]))
@@ -130,16 +131,6 @@ static int phylink_parse_fixedlink(struc
 	}
 
 	if (pl->link_an_mode == MLO_AN_FIXED) {
-		/* Generate the supported/advertising masks */
-		if (pl->link_config.pause & MLO_PAUSE_SYM) {
-			pl->link_config.supported |= SUPPORTED_Pause;
-			pl->link_config.advertising |= ADVERTISED_Pause;
-		}
-		if (pl->link_config.pause & MLO_PAUSE_ASYM) {
-			pl->link_config.supported |= SUPPORTED_Asym_Pause;
-			pl->link_config.advertising |= ADVERTISED_Asym_Pause;
-		}
-
 		if (pl->link_config.speed > SPEED_1000 &&
 		    pl->link_config.duplex != DUPLEX_FULL)
 			netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
@@ -242,6 +233,56 @@ static void phylink_get_fixed_state(stru
 		state->link = !!gpiod_get_value(pl->link_gpio);
 }
 
+/* Flow control is resolved according to our and the link partners
+ * advertisments using the following drawn from the 802.3 specs:
+ *  Local device  Link partner
+ *  Pause AsymDir Pause AsymDir Result
+ *    1     X       1     X     TX+RX
+ *    0     1       1     1     RX
+ *    1     1       0     1     TX
+ */
+static void phylink_resolve_flow(struct phylink *pl,
+	struct phylink_link_state *state)
+{
+	int new_pause = 0;
+
+	if (pl->link_config.pause & MLO_PAUSE_AN) {
+		int pause = 0;
+
+		if (pl->link_config.advertising & ADVERTISED_Pause)
+			pause |= MLO_PAUSE_SYM;
+		if (pl->link_config.advertising & ADVERTISED_Asym_Pause)
+			pause |= MLO_PAUSE_ASYM;
+
+		pause &= state->pause;
+
+		if (pause & MLO_PAUSE_SYM)
+			new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
+		else if (pause & MLO_PAUSE_ASYM)
+			new_pause = state->pause & MLO_PAUSE_SYM ?
+				 MLO_PAUSE_RX : MLO_PAUSE_TX;
+	} else {
+		new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
+	}
+
+	state->pause &= ~MLO_PAUSE_TXRX_MASK;
+	state->pause |= new_pause;
+}
+
+static const char *phylink_pause_to_str(int pause)
+{
+	switch (pause & MLO_PAUSE_TXRX_MASK) {
+	case MLO_PAUSE_TX | MLO_PAUSE_RX:
+		return "rx/tx";
+	case MLO_PAUSE_TX:
+		return "tx";
+	case MLO_PAUSE_RX:
+		return "rx";
+	default:
+		return "off";
+	}
+}
+
 extern const char *phy_speed_to_str(int speed);
 
 static void phylink_resolve(struct work_struct *w)
@@ -257,6 +298,7 @@ static void phylink_resolve(struct work_
 		switch (pl->link_an_mode) {
 		case MLO_AN_PHY:
 			link_state = pl->phy_state;
+			phylink_resolve_flow(pl, &link_state);
 			break;
 
 		case MLO_AN_FIXED:
@@ -265,9 +307,12 @@ static void phylink_resolve(struct work_
 
 		case MLO_AN_SGMII:
 			phylink_get_mac_state(pl, &link_state);
-			if (pl->phydev)
+			if (pl->phydev) {
 				link_state.link = link_state.link &&
 						  pl->phy_state.link;
+				link_state.pause |= pl->phy_state.pause;
+				phylink_resolve_flow(pl, &link_state);
+			}
 			break;
 
 		case MLO_AN_8023Z:
@@ -297,7 +342,7 @@ static void phylink_resolve(struct work_
 				    "Link is Up - %s/%s - flow control %s\n",
 				    phy_speed_to_str(link_state.speed),
 				    link_state.duplex ? "Full" : "Half",
-				    link_state.pause ? "rx/tx" : "off");
+				    phylink_pause_to_str(link_state.pause));
 		}
 	}
 	mutex_unlock(&pl->state_mutex);
@@ -326,6 +371,7 @@ struct phylink *phylink_create(struct ne
 	pl->link_interface = iface;
 	pl->link_port_support = SUPPORTED_MII;
 	pl->link_port = PORT_MII;
+	pl->link_config.pause = MLO_PAUSE_AN;
 	pl->ops = ops;
 	__set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
 
@@ -507,6 +553,7 @@ void phylink_start(struct phylink *pl)
 	 * a fixed-link to start with the correct parameters, and also
 	 * ensures that we set the appropriate advertisment for Serdes links.
 	 */
+	phylink_resolve_flow(pl, &pl->link_config);
 	phylink_mac_config(pl, &pl->link_config);
 
 	clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
@@ -701,6 +748,76 @@ int phylink_ethtool_nway_reset(struct ph
 }
 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
 
+void phylink_ethtool_get_pauseparam(struct phylink *pl,
+				    struct ethtool_pauseparam *pause)
+{
+	mutex_lock(&pl->config_mutex);
+
+	pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
+	pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
+	pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
+
+	mutex_unlock(&pl->config_mutex);
+}
+EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
+
+static int __phylink_ethtool_set_pauseparam(struct phylink *pl,
+					    struct ethtool_pauseparam *pause)
+{
+	struct phylink_link_state *config = &pl->link_config;
+
+	if (!(config->supported & (SUPPORTED_Pause | SUPPORTED_Asym_Pause)))
+		return -EOPNOTSUPP;
+
+	if (!(config->supported & SUPPORTED_Asym_Pause) &&
+	    !pause->autoneg && pause->rx_pause != pause->tx_pause)
+		return -EINVAL;
+
+	config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
+
+	if (pause->autoneg)
+		config->pause |= MLO_PAUSE_AN;
+	if (pause->rx_pause)
+		config->pause |= MLO_PAUSE_RX;
+	if (pause->tx_pause)
+		config->pause |= MLO_PAUSE_TX;
+
+	switch (pl->link_an_mode) {
+	case MLO_AN_PHY:
+		/* Silently mark the carrier down, and then trigger a resolve */
+		netif_carrier_off(pl->netdev);
+		phylink_run_resolve(pl);
+		break;
+
+	case MLO_AN_FIXED:
+		/* Should we allow fixed links to change against the config? */
+		phylink_resolve_flow(pl, config);
+		phylink_mac_config(pl, config);
+		break;
+
+	case MLO_AN_SGMII:
+	case MLO_AN_8023Z:
+		phylink_mac_config(pl, config);
+		phylink_mac_an_restart(pl);
+		break;
+	}
+
+	return 0;
+}
+
+int phylink_ethtool_set_pauseparam(struct phylink *pl,
+				   struct ethtool_pauseparam *pause)
+{
+	int ret;
+
+	mutex_lock(&pl->config_mutex);
+	ret = __phylink_ethtool_set_pauseparam(pl, pause);
+	mutex_unlock(&pl->config_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
+
 /* This emulates MII registers for a fixed-mode phy operating as per the
  * passed in state. "aneg" defines if we report negotiation is possible.
  *
--- a/include/linux/phylink.h
+++ b/include/linux/phylink.h
@@ -13,6 +13,10 @@ enum {
 	MLO_PAUSE_NONE,
 	MLO_PAUSE_ASYM = BIT(0),
 	MLO_PAUSE_SYM = BIT(1),
+	MLO_PAUSE_RX = BIT(2),
+	MLO_PAUSE_TX = BIT(3),
+	MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX,
+	MLO_PAUSE_AN = BIT(4),
 
 	MLO_AN_PHY = 0,
 	MLO_AN_FIXED,
@@ -66,6 +70,10 @@ void phylink_stop(struct phylink *);
 int phylink_ethtool_get_settings(struct phylink *, struct ethtool_cmd *);
 int phylink_ethtool_set_settings(struct phylink *, struct ethtool_cmd *);
 int phylink_ethtool_nway_reset(struct phylink *);
+void phylink_ethtool_get_pauseparam(struct phylink *,
+				    struct ethtool_pauseparam *);
+int phylink_ethtool_set_pauseparam(struct phylink *,
+				   struct ethtool_pauseparam *);
 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
 
 void phylink_set_link_port(struct phylink *pl, u32 support, u8 port);