generic: copy backport, hack, pending patch and config from 6.1 to 6.6
[openwrt/staging/981213.git] / target / linux / generic / backport-6.6 / 715-24-v6.5-net-phylink-add-PCS-negotiation-mode.patch
1 From 79b07c3e9c4a2272927be8848c26b372516e1958 Mon Sep 17 00:00:00 2001
2 From: "Russell King (Oracle)" <rmk+kernel@armlinux.org.uk>
3 Date: Fri, 16 Jun 2023 13:06:22 +0100
4 Subject: [PATCH 21/21] net: phylink: add PCS negotiation mode
5
6 PCS have to work out whether they should enable PCS negotiation by
7 looking at the "mode" and "interface" arguments, and the Autoneg bit
8 in the advertising mask.
9
10 This leads to some complex logic, so lets pull that out into phylink
11 and instead pass a "neg_mode" argument to the PCS configuration and
12 link up methods, instead of the "mode" argument.
13
14 In order to transition drivers, add a "neg_mode" flag to the phylink
15 PCS structure to PCS can indicate whether they want to be passed the
16 neg_mode or the old mode argument.
17
18 Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
19 Link: https://lore.kernel.org/r/E1qA8De-00EaFA-Ht@rmk-PC.armlinux.org.uk
20 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
21 ---
22 drivers/net/phy/phylink.c | 45 +++++++++++++----
23 include/linux/phylink.h | 104 +++++++++++++++++++++++++++++++++++---
24 2 files changed, 132 insertions(+), 17 deletions(-)
25
26 --- a/drivers/net/phy/phylink.c
27 +++ b/drivers/net/phy/phylink.c
28 @@ -71,6 +71,7 @@ struct phylink {
29 struct mutex state_mutex;
30 struct phylink_link_state phy_state;
31 struct work_struct resolve;
32 + unsigned int pcs_neg_mode;
33
34 bool mac_link_dropped;
35 bool using_mac_select_pcs;
36 @@ -991,23 +992,23 @@ static void phylink_resolve_an_pause(str
37 }
38 }
39
40 -static int phylink_pcs_config(struct phylink_pcs *pcs, unsigned int mode,
41 +static int phylink_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
42 const struct phylink_link_state *state,
43 bool permit_pause_to_mac)
44 {
45 if (!pcs)
46 return 0;
47
48 - return pcs->ops->pcs_config(pcs, mode, state->interface,
49 + return pcs->ops->pcs_config(pcs, neg_mode, state->interface,
50 state->advertising, permit_pause_to_mac);
51 }
52
53 -static void phylink_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
54 +static void phylink_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
55 phy_interface_t interface, int speed,
56 int duplex)
57 {
58 if (pcs && pcs->ops->pcs_link_up)
59 - pcs->ops->pcs_link_up(pcs, mode, interface, speed, duplex);
60 + pcs->ops->pcs_link_up(pcs, neg_mode, interface, speed, duplex);
61 }
62
63 static void phylink_pcs_poll_stop(struct phylink *pl)
64 @@ -1057,10 +1058,15 @@ static void phylink_major_config(struct
65 struct phylink_pcs *pcs = NULL;
66 bool pcs_changed = false;
67 unsigned int rate_kbd;
68 + unsigned int neg_mode;
69 int err;
70
71 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
72
73 + pl->pcs_neg_mode = phylink_pcs_neg_mode(pl->cur_link_an_mode,
74 + state->interface,
75 + state->advertising);
76 +
77 if (pl->using_mac_select_pcs) {
78 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
79 if (IS_ERR(pcs)) {
80 @@ -1093,9 +1099,12 @@ static void phylink_major_config(struct
81
82 phylink_mac_config(pl, state);
83
84 - err = phylink_pcs_config(pl->pcs, pl->cur_link_an_mode, state,
85 - !!(pl->link_config.pause &
86 - MLO_PAUSE_AN));
87 + neg_mode = pl->cur_link_an_mode;
88 + if (pl->pcs && pl->pcs->neg_mode)
89 + neg_mode = pl->pcs_neg_mode;
90 +
91 + err = phylink_pcs_config(pl->pcs, neg_mode, state,
92 + !!(pl->link_config.pause & MLO_PAUSE_AN));
93 if (err < 0)
94 phylink_err(pl, "pcs_config failed: %pe\n",
95 ERR_PTR(err));
96 @@ -1130,6 +1139,7 @@ static void phylink_major_config(struct
97 */
98 static int phylink_change_inband_advert(struct phylink *pl)
99 {
100 + unsigned int neg_mode;
101 int ret;
102
103 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
104 @@ -1148,12 +1158,20 @@ static int phylink_change_inband_advert(
105 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
106 pl->link_config.pause);
107
108 + /* Recompute the PCS neg mode */
109 + pl->pcs_neg_mode = phylink_pcs_neg_mode(pl->cur_link_an_mode,
110 + pl->link_config.interface,
111 + pl->link_config.advertising);
112 +
113 + neg_mode = pl->cur_link_an_mode;
114 + if (pl->pcs->neg_mode)
115 + neg_mode = pl->pcs_neg_mode;
116 +
117 /* Modern PCS-based method; update the advert at the PCS, and
118 * restart negotiation if the pcs_config() helper indicates that
119 * the programmed advertisement has changed.
120 */
121 - ret = phylink_pcs_config(pl->pcs, pl->cur_link_an_mode,
122 - &pl->link_config,
123 + ret = phylink_pcs_config(pl->pcs, neg_mode, &pl->link_config,
124 !!(pl->link_config.pause & MLO_PAUSE_AN));
125 if (ret < 0)
126 return ret;
127 @@ -1256,6 +1274,7 @@ static void phylink_link_up(struct phyli
128 struct phylink_link_state link_state)
129 {
130 struct net_device *ndev = pl->netdev;
131 + unsigned int neg_mode;
132 int speed, duplex;
133 bool rx_pause;
134
135 @@ -1286,8 +1305,12 @@ static void phylink_link_up(struct phyli
136
137 pl->cur_interface = link_state.interface;
138
139 - phylink_pcs_link_up(pl->pcs, pl->cur_link_an_mode, pl->cur_interface,
140 - speed, duplex);
141 + neg_mode = pl->cur_link_an_mode;
142 + if (pl->pcs && pl->pcs->neg_mode)
143 + neg_mode = pl->pcs_neg_mode;
144 +
145 + phylink_pcs_link_up(pl->pcs, neg_mode, pl->cur_interface, speed,
146 + duplex);
147
148 pl->mac_ops->mac_link_up(pl->config, pl->phydev, pl->cur_link_an_mode,
149 pl->cur_interface, speed, duplex,
150 --- a/include/linux/phylink.h
151 +++ b/include/linux/phylink.h
152 @@ -21,6 +21,24 @@ enum {
153 MLO_AN_FIXED, /* Fixed-link mode */
154 MLO_AN_INBAND, /* In-band protocol */
155
156 + /* PCS "negotiation" mode.
157 + * PHYLINK_PCS_NEG_NONE - protocol has no inband capability
158 + * PHYLINK_PCS_NEG_OUTBAND - some out of band or fixed link setting
159 + * PHYLINK_PCS_NEG_INBAND_DISABLED - inband mode disabled, e.g.
160 + * 1000base-X with autoneg off
161 + * PHYLINK_PCS_NEG_INBAND_ENABLED - inband mode enabled
162 + * Additionally, this can be tested using bitmasks:
163 + * PHYLINK_PCS_NEG_INBAND - inband mode selected
164 + * PHYLINK_PCS_NEG_ENABLED - negotiation mode enabled
165 + */
166 + PHYLINK_PCS_NEG_NONE = 0,
167 + PHYLINK_PCS_NEG_ENABLED = BIT(4),
168 + PHYLINK_PCS_NEG_OUTBAND = BIT(5),
169 + PHYLINK_PCS_NEG_INBAND = BIT(6),
170 + PHYLINK_PCS_NEG_INBAND_DISABLED = PHYLINK_PCS_NEG_INBAND,
171 + PHYLINK_PCS_NEG_INBAND_ENABLED = PHYLINK_PCS_NEG_INBAND |
172 + PHYLINK_PCS_NEG_ENABLED,
173 +
174 /* MAC_SYM_PAUSE and MAC_ASYM_PAUSE are used when configuring our
175 * autonegotiation advertisement. They correspond to the PAUSE and
176 * ASM_DIR bits defined by 802.3, respectively.
177 @@ -80,6 +98,70 @@ static inline bool phylink_autoneg_inban
178 }
179
180 /**
181 + * phylink_pcs_neg_mode() - helper to determine PCS inband mode
182 + * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
183 + * @interface: interface mode to be used
184 + * @advertising: adertisement ethtool link mode mask
185 + *
186 + * Determines the negotiation mode to be used by the PCS, and returns
187 + * one of:
188 + * %PHYLINK_PCS_NEG_NONE: interface mode does not support inband
189 + * %PHYLINK_PCS_NEG_OUTBAND: an out of band mode (e.g. reading the PHY)
190 + * will be used.
191 + * %PHYLINK_PCS_NEG_INBAND_DISABLED: inband mode selected but autoneg disabled
192 + * %PHYLINK_PCS_NEG_INBAND_ENABLED: inband mode selected and autoneg enabled
193 + *
194 + * Note: this is for cases where the PCS itself is involved in negotiation
195 + * (e.g. Clause 37, SGMII and similar) not Clause 73.
196 + */
197 +static inline unsigned int phylink_pcs_neg_mode(unsigned int mode,
198 + phy_interface_t interface,
199 + const unsigned long *advertising)
200 +{
201 + unsigned int neg_mode;
202 +
203 + switch (interface) {
204 + case PHY_INTERFACE_MODE_SGMII:
205 + case PHY_INTERFACE_MODE_QSGMII:
206 + case PHY_INTERFACE_MODE_QUSGMII:
207 + case PHY_INTERFACE_MODE_USXGMII:
208 + /* These protocols are designed for use with a PHY which
209 + * communicates its negotiation result back to the MAC via
210 + * inband communication. Note: there exist PHYs that run
211 + * with SGMII but do not send the inband data.
212 + */
213 + if (!phylink_autoneg_inband(mode))
214 + neg_mode = PHYLINK_PCS_NEG_OUTBAND;
215 + else
216 + neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
217 + break;
218 +
219 + case PHY_INTERFACE_MODE_1000BASEX:
220 + case PHY_INTERFACE_MODE_2500BASEX:
221 + /* 1000base-X is designed for use media-side for Fibre
222 + * connections, and thus the Autoneg bit needs to be
223 + * taken into account. We also do this for 2500base-X
224 + * as well, but drivers may not support this, so may
225 + * need to override this.
226 + */
227 + if (!phylink_autoneg_inband(mode))
228 + neg_mode = PHYLINK_PCS_NEG_OUTBAND;
229 + else if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
230 + advertising))
231 + neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
232 + else
233 + neg_mode = PHYLINK_PCS_NEG_INBAND_DISABLED;
234 + break;
235 +
236 + default:
237 + neg_mode = PHYLINK_PCS_NEG_NONE;
238 + break;
239 + }
240 +
241 + return neg_mode;
242 +}
243 +
244 +/**
245 * struct phylink_link_state - link state structure
246 * @advertising: ethtool bitmask containing advertised link modes
247 * @lp_advertising: ethtool bitmask containing link partner advertised link
248 @@ -436,6 +518,7 @@ struct phylink_pcs_ops;
249 /**
250 * struct phylink_pcs - PHYLINK PCS instance
251 * @ops: a pointer to the &struct phylink_pcs_ops structure
252 + * @neg_mode: provide PCS neg mode via "mode" argument
253 * @poll: poll the PCS for link changes
254 *
255 * This structure is designed to be embedded within the PCS private data,
256 @@ -443,6 +526,7 @@ struct phylink_pcs_ops;
257 */
258 struct phylink_pcs {
259 const struct phylink_pcs_ops *ops;
260 + bool neg_mode;
261 bool poll;
262 };
263
264 @@ -460,12 +544,12 @@ struct phylink_pcs_ops {
265 const struct phylink_link_state *state);
266 void (*pcs_get_state)(struct phylink_pcs *pcs,
267 struct phylink_link_state *state);
268 - int (*pcs_config)(struct phylink_pcs *pcs, unsigned int mode,
269 + int (*pcs_config)(struct phylink_pcs *pcs, unsigned int neg_mode,
270 phy_interface_t interface,
271 const unsigned long *advertising,
272 bool permit_pause_to_mac);
273 void (*pcs_an_restart)(struct phylink_pcs *pcs);
274 - void (*pcs_link_up)(struct phylink_pcs *pcs, unsigned int mode,
275 + void (*pcs_link_up)(struct phylink_pcs *pcs, unsigned int neg_mode,
276 phy_interface_t interface, int speed, int duplex);
277 };
278
279 @@ -508,7 +592,7 @@ void pcs_get_state(struct phylink_pcs *p
280 /**
281 * pcs_config() - Configure the PCS mode and advertisement
282 * @pcs: a pointer to a &struct phylink_pcs.
283 - * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
284 + * @neg_mode: link negotiation mode (see below)
285 * @interface: interface mode to be used
286 * @advertising: adertisement ethtool link mode mask
287 * @permit_pause_to_mac: permit forwarding pause resolution to MAC
288 @@ -526,8 +610,12 @@ void pcs_get_state(struct phylink_pcs *p
289 * For 1000BASE-X, the advertisement should be programmed into the PCS.
290 *
291 * For most 10GBASE-R, there is no advertisement.
292 + *
293 + * The %neg_mode argument should be tested via the phylink_mode_*() family of
294 + * functions, or for PCS that set pcs->neg_mode true, should be tested
295 + * against the %PHYLINK_PCS_NEG_* definitions.
296 */
297 -int pcs_config(struct phylink_pcs *pcs, unsigned int mode,
298 +int pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
299 phy_interface_t interface, const unsigned long *advertising,
300 bool permit_pause_to_mac);
301
302 @@ -543,7 +631,7 @@ void pcs_an_restart(struct phylink_pcs *
303 /**
304 * pcs_link_up() - program the PCS for the resolved link configuration
305 * @pcs: a pointer to a &struct phylink_pcs.
306 - * @mode: link autonegotiation mode
307 + * @neg_mode: link negotiation mode (see below)
308 * @interface: link &typedef phy_interface_t mode
309 * @speed: link speed
310 * @duplex: link duplex
311 @@ -552,8 +640,12 @@ void pcs_an_restart(struct phylink_pcs *
312 * the resolved link parameters. For example, a PCS operating in SGMII
313 * mode without in-band AN needs to be manually configured for the link
314 * and duplex setting. Otherwise, this should be a no-op.
315 + *
316 + * The %mode argument should be tested via the phylink_mode_*() family of
317 + * functions, or for PCS that set pcs->neg_mode true, should be tested
318 + * against the %PHYLINK_PCS_NEG_* definitions.
319 */
320 -void pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
321 +void pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
322 phy_interface_t interface, int speed, int duplex);
323 #endif
324