blob: 6cb9ca74341bfce84c3846de1f13ef5b638d5c41 [file] [log] [blame]
Andrew Lunn5f857572019-01-21 19:10:19 +01001// SPDX-License-Identifier: GPL-2.0
Russell King9525ae82017-07-25 15:03:13 +01002/*
3 * phylink models the MAC to optional PHY connection, supporting
4 * technologies such as SFP cages where the PHY is hot-pluggable.
5 *
6 * Copyright (C) 2015 Russell King
Russell King9525ae82017-07-25 15:03:13 +01007 */
8#include <linux/ethtool.h>
9#include <linux/export.h>
10#include <linux/gpio/consumer.h>
11#include <linux/netdevice.h>
12#include <linux/of.h>
13#include <linux/of_mdio.h>
14#include <linux/phy.h>
15#include <linux/phy_fixed.h>
16#include <linux/phylink.h>
17#include <linux/rtnetlink.h>
18#include <linux/spinlock.h>
Russell King9cd00a82018-05-10 13:17:31 -070019#include <linux/timer.h>
Russell King9525ae82017-07-25 15:03:13 +010020#include <linux/workqueue.h>
21
Russell Kingce0aa272017-07-25 15:03:18 +010022#include "sfp.h"
Russell King9525ae82017-07-25 15:03:13 +010023#include "swphy.h"
24
25#define SUPPORTED_INTERFACES \
26 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
27 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
28#define ADVERTISED_INTERFACES \
29 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
30 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
31
32enum {
33 PHYLINK_DISABLE_STOPPED,
Russell Kingce0aa272017-07-25 15:03:18 +010034 PHYLINK_DISABLE_LINK,
Russell King9525ae82017-07-25 15:03:13 +010035};
36
Russell King8796c892017-12-01 10:24:47 +000037/**
38 * struct phylink - internal data type for phylink
39 */
Russell King9525ae82017-07-25 15:03:13 +010040struct phylink {
Russell King8796c892017-12-01 10:24:47 +000041 /* private: */
Russell King9525ae82017-07-25 15:03:13 +010042 struct net_device *netdev;
Russell Kinge7765d62020-03-30 18:44:50 +010043 const struct phylink_mac_ops *mac_ops;
Russell King4c0d6d32020-03-30 18:44:55 +010044 const struct phylink_pcs_ops *pcs_ops;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +030045 struct phylink_config *config;
Ioana Ciornei43de6192019-05-28 20:38:13 +030046 struct device *dev;
47 unsigned int old_link_state:1;
Russell King9525ae82017-07-25 15:03:13 +010048
49 unsigned long phylink_disable_state; /* bitmask of disables */
50 struct phy_device *phydev;
51 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
Russell King24cf0e62019-12-11 10:56:35 +000052 u8 cfg_link_an_mode; /* MLO_AN_xxx */
53 u8 cur_link_an_mode;
Russell King9525ae82017-07-25 15:03:13 +010054 u8 link_port; /* The current non-phy ethtool port */
55 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
56
57 /* The link configuration settings */
58 struct phylink_link_state link_config;
Russell Kingc6787262019-05-28 10:27:21 +010059
60 /* The current settings */
61 phy_interface_t cur_interface;
62
Russell King9525ae82017-07-25 15:03:13 +010063 struct gpio_desc *link_gpio;
Russell King7b3b0e82019-05-28 10:57:23 +010064 unsigned int link_irq;
Russell King9cd00a82018-05-10 13:17:31 -070065 struct timer_list link_poll;
Florian Fainelli1ac63e32017-12-12 16:00:28 -080066 void (*get_fixed_state)(struct net_device *dev,
67 struct phylink_link_state *s);
Russell King9525ae82017-07-25 15:03:13 +010068
69 struct mutex state_mutex;
70 struct phylink_link_state phy_state;
71 struct work_struct resolve;
72
73 bool mac_link_dropped;
Russell Kingce0aa272017-07-25 15:03:18 +010074
75 struct sfp_bus *sfp_bus;
Russell King52c95602019-12-11 10:56:45 +000076 bool sfp_may_have_phy;
77 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
78 u8 sfp_port;
Russell King9525ae82017-07-25 15:03:13 +010079};
80
Ioana Ciornei170911802019-05-28 20:38:14 +030081#define phylink_printk(level, pl, fmt, ...) \
82 do { \
83 if ((pl)->config->type == PHYLINK_NETDEV) \
84 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
85 else if ((pl)->config->type == PHYLINK_DEV) \
86 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
87 } while (0)
88
89#define phylink_err(pl, fmt, ...) \
90 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
91#define phylink_warn(pl, fmt, ...) \
92 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
93#define phylink_info(pl, fmt, ...) \
94 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
Florian Fainelli9d68db52019-10-31 15:42:26 -070095#if defined(CONFIG_DYNAMIC_DEBUG)
Ioana Ciornei170911802019-05-28 20:38:14 +030096#define phylink_dbg(pl, fmt, ...) \
Florian Fainelli9d68db52019-10-31 15:42:26 -070097do { \
98 if ((pl)->config->type == PHYLINK_NETDEV) \
99 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
100 else if ((pl)->config->type == PHYLINK_DEV) \
101 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
102} while (0)
103#elif defined(DEBUG)
104#define phylink_dbg(pl, fmt, ...) \
Ioana Ciornei170911802019-05-28 20:38:14 +0300105 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
Florian Fainelli9d68db52019-10-31 15:42:26 -0700106#else
107#define phylink_dbg(pl, fmt, ...) \
108({ \
109 if (0) \
110 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
111})
112#endif
Ioana Ciornei170911802019-05-28 20:38:14 +0300113
Russell King8796c892017-12-01 10:24:47 +0000114/**
115 * phylink_set_port_modes() - set the port type modes in the ethtool mask
116 * @mask: ethtool link mode mask
117 *
118 * Sets all the port type modes in the ethtool mask. MAC drivers should
119 * use this in their 'validate' callback.
120 */
Russell King9525ae82017-07-25 15:03:13 +0100121void phylink_set_port_modes(unsigned long *mask)
122{
123 phylink_set(mask, TP);
124 phylink_set(mask, AUI);
125 phylink_set(mask, MII);
126 phylink_set(mask, FIBRE);
127 phylink_set(mask, BNC);
128 phylink_set(mask, Backplane);
129}
130EXPORT_SYMBOL_GPL(phylink_set_port_modes);
131
132static int phylink_is_empty_linkmode(const unsigned long *linkmode)
133{
134 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
135
136 phylink_set_port_modes(tmp);
137 phylink_set(tmp, Autoneg);
138 phylink_set(tmp, Pause);
139 phylink_set(tmp, Asym_Pause);
140
Russell King554032c2019-10-15 11:28:46 +0100141 return linkmode_subset(linkmode, tmp);
Russell King9525ae82017-07-25 15:03:13 +0100142}
143
144static const char *phylink_an_mode_str(unsigned int mode)
145{
146 static const char *modestr[] = {
147 [MLO_AN_PHY] = "phy",
148 [MLO_AN_FIXED] = "fixed",
Russell King86a362c2017-12-01 10:24:26 +0000149 [MLO_AN_INBAND] = "inband",
Russell King9525ae82017-07-25 15:03:13 +0100150 };
151
152 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
153}
154
155static int phylink_validate(struct phylink *pl, unsigned long *supported,
156 struct phylink_link_state *state)
157{
Russell Kinge7765d62020-03-30 18:44:50 +0100158 pl->mac_ops->validate(pl->config, supported, state);
Russell King9525ae82017-07-25 15:03:13 +0100159
160 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
161}
162
Russell King8fa7b9b2017-12-01 10:25:09 +0000163static int phylink_parse_fixedlink(struct phylink *pl,
164 struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100165{
Russell King8fa7b9b2017-12-01 10:25:09 +0000166 struct fwnode_handle *fixed_node;
Russell King9525ae82017-07-25 15:03:13 +0100167 const struct phy_setting *s;
168 struct gpio_desc *desc;
Russell King9525ae82017-07-25 15:03:13 +0100169 u32 speed;
Russell King8fa7b9b2017-12-01 10:25:09 +0000170 int ret;
Russell King9525ae82017-07-25 15:03:13 +0100171
Russell King8fa7b9b2017-12-01 10:25:09 +0000172 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
Russell King9525ae82017-07-25 15:03:13 +0100173 if (fixed_node) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000174 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
Russell King9525ae82017-07-25 15:03:13 +0100175
176 pl->link_config.speed = speed;
177 pl->link_config.duplex = DUPLEX_HALF;
178
Russell King8fa7b9b2017-12-01 10:25:09 +0000179 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
Russell King9525ae82017-07-25 15:03:13 +0100180 pl->link_config.duplex = DUPLEX_FULL;
181
182 /* We treat the "pause" and "asym-pause" terminology as
183 * defining the link partner's ability. */
Russell King8fa7b9b2017-12-01 10:25:09 +0000184 if (fwnode_property_read_bool(fixed_node, "pause"))
Russell King4e5aeb42020-02-15 15:49:53 +0000185 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
186 pl->link_config.lp_advertising);
Russell King8fa7b9b2017-12-01 10:25:09 +0000187 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
Russell King4e5aeb42020-02-15 15:49:53 +0000188 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
189 pl->link_config.lp_advertising);
Russell King9525ae82017-07-25 15:03:13 +0100190
191 if (ret == 0) {
Dmitry Torokhovb605c9a2020-01-02 17:03:18 -0800192 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
193 GPIOD_IN, "?");
Russell King9525ae82017-07-25 15:03:13 +0100194
195 if (!IS_ERR(desc))
196 pl->link_gpio = desc;
197 else if (desc == ERR_PTR(-EPROBE_DEFER))
198 ret = -EPROBE_DEFER;
199 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000200 fwnode_handle_put(fixed_node);
Russell King9525ae82017-07-25 15:03:13 +0100201
202 if (ret)
203 return ret;
204 } else {
Russell King8fa7b9b2017-12-01 10:25:09 +0000205 u32 prop[5];
206
207 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
208 NULL, 0);
209 if (ret != ARRAY_SIZE(prop)) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300210 phylink_err(pl, "broken fixed-link?\n");
Russell King9525ae82017-07-25 15:03:13 +0100211 return -EINVAL;
212 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000213
214 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
215 prop, ARRAY_SIZE(prop));
216 if (!ret) {
217 pl->link_config.duplex = prop[1] ?
Russell King9525ae82017-07-25 15:03:13 +0100218 DUPLEX_FULL : DUPLEX_HALF;
Russell King8fa7b9b2017-12-01 10:25:09 +0000219 pl->link_config.speed = prop[2];
220 if (prop[3])
Russell King4e5aeb42020-02-15 15:49:53 +0000221 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
222 pl->link_config.lp_advertising);
Russell King8fa7b9b2017-12-01 10:25:09 +0000223 if (prop[4])
Russell King4e5aeb42020-02-15 15:49:53 +0000224 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
225 pl->link_config.lp_advertising);
Russell King9525ae82017-07-25 15:03:13 +0100226 }
227 }
228
229 if (pl->link_config.speed > SPEED_1000 &&
230 pl->link_config.duplex != DUPLEX_FULL)
Ioana Ciornei170911802019-05-28 20:38:14 +0300231 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
232 pl->link_config.speed);
Russell King9525ae82017-07-25 15:03:13 +0100233
234 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
235 linkmode_copy(pl->link_config.advertising, pl->supported);
236 phylink_validate(pl, pl->supported, &pl->link_config);
237
238 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100239 pl->supported, true);
Russell King9525ae82017-07-25 15:03:13 +0100240 linkmode_zero(pl->supported);
241 phylink_set(pl->supported, MII);
René van Dorst8aace4f2019-07-27 11:40:11 +0200242 phylink_set(pl->supported, Pause);
243 phylink_set(pl->supported, Asym_Pause);
Russell King1ceb7ee2020-07-21 12:03:45 +0100244 phylink_set(pl->supported, Autoneg);
Russell King9525ae82017-07-25 15:03:13 +0100245 if (s) {
246 __set_bit(s->bit, pl->supported);
Russell King1ceb7ee2020-07-21 12:03:45 +0100247 __set_bit(s->bit, pl->link_config.lp_advertising);
Russell King9525ae82017-07-25 15:03:13 +0100248 } else {
Ioana Ciornei170911802019-05-28 20:38:14 +0300249 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
250 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
251 pl->link_config.speed);
Russell King9525ae82017-07-25 15:03:13 +0100252 }
253
254 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
255 pl->supported);
256
257 pl->link_config.link = 1;
258 pl->link_config.an_complete = 1;
259
260 return 0;
261}
262
Russell King8fa7b9b2017-12-01 10:25:09 +0000263static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100264{
Russell King8fa7b9b2017-12-01 10:25:09 +0000265 struct fwnode_handle *dn;
Russell King9525ae82017-07-25 15:03:13 +0100266 const char *managed;
267
Russell King8fa7b9b2017-12-01 10:25:09 +0000268 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
269 if (dn || fwnode_property_present(fwnode, "fixed-link"))
Russell King24cf0e62019-12-11 10:56:35 +0000270 pl->cfg_link_an_mode = MLO_AN_FIXED;
Russell King8fa7b9b2017-12-01 10:25:09 +0000271 fwnode_handle_put(dn);
Russell King9525ae82017-07-25 15:03:13 +0100272
Russell King8fa7b9b2017-12-01 10:25:09 +0000273 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
Russell King9525ae82017-07-25 15:03:13 +0100274 strcmp(managed, "in-band-status") == 0) {
Russell King24cf0e62019-12-11 10:56:35 +0000275 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300276 phylink_err(pl,
277 "can't use both fixed-link and in-band-status\n");
Russell King9525ae82017-07-25 15:03:13 +0100278 return -EINVAL;
279 }
280
281 linkmode_zero(pl->supported);
282 phylink_set(pl->supported, MII);
283 phylink_set(pl->supported, Autoneg);
284 phylink_set(pl->supported, Asym_Pause);
285 phylink_set(pl->supported, Pause);
286 pl->link_config.an_enabled = true;
Russell King24cf0e62019-12-11 10:56:35 +0000287 pl->cfg_link_an_mode = MLO_AN_INBAND;
Russell King9525ae82017-07-25 15:03:13 +0100288
289 switch (pl->link_config.interface) {
290 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean3a68ba62020-01-06 03:34:10 +0200291 case PHY_INTERFACE_MODE_QSGMII:
Russell King9525ae82017-07-25 15:03:13 +0100292 phylink_set(pl->supported, 10baseT_Half);
293 phylink_set(pl->supported, 10baseT_Full);
294 phylink_set(pl->supported, 100baseT_Half);
295 phylink_set(pl->supported, 100baseT_Full);
296 phylink_set(pl->supported, 1000baseT_Half);
297 phylink_set(pl->supported, 1000baseT_Full);
Russell King9525ae82017-07-25 15:03:13 +0100298 break;
299
300 case PHY_INTERFACE_MODE_1000BASEX:
301 phylink_set(pl->supported, 1000baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100302 break;
303
304 case PHY_INTERFACE_MODE_2500BASEX:
305 phylink_set(pl->supported, 2500baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100306 break;
307
Alex Marginean04e22462020-01-18 14:19:15 +0200308 case PHY_INTERFACE_MODE_USXGMII:
Russell Kingda7c1862017-07-25 15:03:34 +0100309 case PHY_INTERFACE_MODE_10GKR:
Russell Kinge0f909b2020-01-03 20:43:23 +0000310 case PHY_INTERFACE_MODE_10GBASER:
Russell Kingda7c1862017-07-25 15:03:34 +0100311 phylink_set(pl->supported, 10baseT_Half);
312 phylink_set(pl->supported, 10baseT_Full);
313 phylink_set(pl->supported, 100baseT_Half);
314 phylink_set(pl->supported, 100baseT_Full);
315 phylink_set(pl->supported, 1000baseT_Half);
316 phylink_set(pl->supported, 1000baseT_Full);
317 phylink_set(pl->supported, 1000baseX_Full);
Jose Abreuc5801652020-03-09 09:36:24 +0100318 phylink_set(pl->supported, 1000baseKX_Full);
Vladimir Oltean6cbdcf22020-01-16 19:36:56 +0200319 phylink_set(pl->supported, 2500baseT_Full);
320 phylink_set(pl->supported, 2500baseX_Full);
321 phylink_set(pl->supported, 5000baseT_Full);
322 phylink_set(pl->supported, 10000baseT_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100323 phylink_set(pl->supported, 10000baseKR_Full);
Jose Abreuc5801652020-03-09 09:36:24 +0100324 phylink_set(pl->supported, 10000baseKX4_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100325 phylink_set(pl->supported, 10000baseCR_Full);
326 phylink_set(pl->supported, 10000baseSR_Full);
327 phylink_set(pl->supported, 10000baseLR_Full);
328 phylink_set(pl->supported, 10000baseLRM_Full);
329 phylink_set(pl->supported, 10000baseER_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100330 break;
331
Jose Abreu1671c422020-03-12 18:10:10 +0100332 case PHY_INTERFACE_MODE_XLGMII:
333 phylink_set(pl->supported, 25000baseCR_Full);
334 phylink_set(pl->supported, 25000baseKR_Full);
335 phylink_set(pl->supported, 25000baseSR_Full);
336 phylink_set(pl->supported, 40000baseKR4_Full);
337 phylink_set(pl->supported, 40000baseCR4_Full);
338 phylink_set(pl->supported, 40000baseSR4_Full);
339 phylink_set(pl->supported, 40000baseLR4_Full);
340 phylink_set(pl->supported, 50000baseCR2_Full);
341 phylink_set(pl->supported, 50000baseKR2_Full);
342 phylink_set(pl->supported, 50000baseSR2_Full);
343 phylink_set(pl->supported, 50000baseKR_Full);
344 phylink_set(pl->supported, 50000baseSR_Full);
345 phylink_set(pl->supported, 50000baseCR_Full);
346 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
347 phylink_set(pl->supported, 50000baseDR_Full);
348 phylink_set(pl->supported, 100000baseKR4_Full);
349 phylink_set(pl->supported, 100000baseSR4_Full);
350 phylink_set(pl->supported, 100000baseCR4_Full);
351 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
352 phylink_set(pl->supported, 100000baseKR2_Full);
353 phylink_set(pl->supported, 100000baseSR2_Full);
354 phylink_set(pl->supported, 100000baseCR2_Full);
355 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
356 phylink_set(pl->supported, 100000baseDR2_Full);
357 break;
358
Russell King9525ae82017-07-25 15:03:13 +0100359 default:
Ioana Ciornei170911802019-05-28 20:38:14 +0300360 phylink_err(pl,
361 "incorrect link mode %s for in-band status\n",
362 phy_modes(pl->link_config.interface));
Russell King9525ae82017-07-25 15:03:13 +0100363 return -EINVAL;
364 }
365
366 linkmode_copy(pl->link_config.advertising, pl->supported);
367
368 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300369 phylink_err(pl,
370 "failed to validate link configuration for in-band status\n");
Russell King9525ae82017-07-25 15:03:13 +0100371 return -EINVAL;
372 }
Jose Abreu94148192020-03-09 09:36:25 +0100373
374 /* Check if MAC/PCS also supports Autoneg. */
375 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
Russell King9525ae82017-07-25 15:03:13 +0100376 }
377
378 return 0;
379}
380
Russell King2d5fbef2020-02-15 15:49:43 +0000381static void phylink_apply_manual_flow(struct phylink *pl,
382 struct phylink_link_state *state)
383{
384 /* If autoneg is disabled, pause AN is also disabled */
385 if (!state->an_enabled)
386 state->pause &= ~MLO_PAUSE_AN;
387
388 /* Manual configuration of pause modes */
389 if (!(pl->link_config.pause & MLO_PAUSE_AN))
390 state->pause = pl->link_config.pause;
391}
392
Russell King4e5aeb42020-02-15 15:49:53 +0000393static void phylink_resolve_flow(struct phylink_link_state *state)
394{
395 bool tx_pause, rx_pause;
396
397 state->pause = MLO_PAUSE_NONE;
398 if (state->duplex == DUPLEX_FULL) {
399 linkmode_resolve_pause(state->advertising,
400 state->lp_advertising,
401 &tx_pause, &rx_pause);
402 if (tx_pause)
403 state->pause |= MLO_PAUSE_TX;
404 if (rx_pause)
405 state->pause |= MLO_PAUSE_RX;
406 }
407}
408
Russell King9525ae82017-07-25 15:03:13 +0100409static void phylink_mac_config(struct phylink *pl,
410 const struct phylink_link_state *state)
411{
Ioana Ciornei170911802019-05-28 20:38:14 +0300412 phylink_dbg(pl,
413 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
Russell King24cf0e62019-12-11 10:56:35 +0000414 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
Ioana Ciornei170911802019-05-28 20:38:14 +0300415 phy_modes(state->interface),
416 phy_speed_to_str(state->speed),
417 phy_duplex_to_str(state->duplex),
418 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
419 state->pause, state->link, state->an_enabled);
Russell King9525ae82017-07-25 15:03:13 +0100420
Russell Kinge7765d62020-03-30 18:44:50 +0100421 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
Russell King9525ae82017-07-25 15:03:13 +0100422}
423
Russell King4c0d6d32020-03-30 18:44:55 +0100424static void phylink_mac_pcs_an_restart(struct phylink *pl)
Russell King9525ae82017-07-25 15:03:13 +0100425{
426 if (pl->link_config.an_enabled &&
Russell King575691b2020-06-24 12:30:04 +0100427 phy_interface_mode_is_8023z(pl->link_config.interface) &&
428 phylink_autoneg_inband(pl->cur_link_an_mode)) {
Russell King4c0d6d32020-03-30 18:44:55 +0100429 if (pl->pcs_ops)
430 pl->pcs_ops->pcs_an_restart(pl->config);
431 else
432 pl->mac_ops->mac_an_restart(pl->config);
433 }
434}
435
436static void phylink_pcs_config(struct phylink *pl, bool force_restart,
437 const struct phylink_link_state *state)
438{
439 bool restart = force_restart;
440
441 if (pl->pcs_ops && pl->pcs_ops->pcs_config(pl->config,
442 pl->cur_link_an_mode,
443 state->interface,
444 state->advertising))
445 restart = true;
446
447 phylink_mac_config(pl, state);
448
449 if (restart)
450 phylink_mac_pcs_an_restart(pl);
Russell King9525ae82017-07-25 15:03:13 +0100451}
452
Russell Kingd46b7e42019-11-21 00:36:22 +0000453static void phylink_mac_pcs_get_state(struct phylink *pl,
454 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100455{
Russell King9525ae82017-07-25 15:03:13 +0100456 linkmode_copy(state->advertising, pl->link_config.advertising);
457 linkmode_zero(state->lp_advertising);
458 state->interface = pl->link_config.interface;
459 state->an_enabled = pl->link_config.an_enabled;
Heiner Kallweitd25ed412019-02-26 19:29:22 +0100460 state->speed = SPEED_UNKNOWN;
461 state->duplex = DUPLEX_UNKNOWN;
462 state->pause = MLO_PAUSE_NONE;
463 state->an_complete = 0;
Russell King9525ae82017-07-25 15:03:13 +0100464 state->link = 1;
465
Russell King4c0d6d32020-03-30 18:44:55 +0100466 if (pl->pcs_ops)
467 pl->pcs_ops->pcs_get_state(pl->config, state);
468 else
469 pl->mac_ops->mac_pcs_get_state(pl->config, state);
Russell King9525ae82017-07-25 15:03:13 +0100470}
471
472/* The fixed state is... fixed except for the link state,
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800473 * which may be determined by a GPIO or a callback.
Russell King9525ae82017-07-25 15:03:13 +0100474 */
Russell King4e5aeb42020-02-15 15:49:53 +0000475static void phylink_get_fixed_state(struct phylink *pl,
476 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100477{
478 *state = pl->link_config;
Russell King5c05c1d2020-04-23 17:02:56 +0100479 if (pl->config->get_fixed_state)
480 pl->config->get_fixed_state(pl->config, state);
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800481 else if (pl->link_gpio)
Florian Fainellibb322a92018-05-10 13:17:29 -0700482 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
Russell King9525ae82017-07-25 15:03:13 +0100483
Russell King4e5aeb42020-02-15 15:49:53 +0000484 phylink_resolve_flow(state);
Russell King9525ae82017-07-25 15:03:13 +0100485}
486
Russell King4c0d6d32020-03-30 18:44:55 +0100487static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
Russell King97fec512020-02-15 15:50:03 +0000488{
489 struct phylink_link_state link_state;
490
491 switch (pl->cur_link_an_mode) {
492 case MLO_AN_PHY:
493 link_state = pl->phy_state;
494 break;
495
496 case MLO_AN_FIXED:
497 phylink_get_fixed_state(pl, &link_state);
498 break;
499
500 case MLO_AN_INBAND:
501 link_state = pl->link_config;
502 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
503 link_state.pause = MLO_PAUSE_NONE;
504 break;
505
506 default: /* can't happen */
507 return;
508 }
509
510 link_state.link = false;
511
512 phylink_apply_manual_flow(pl, &link_state);
Russell King4c0d6d32020-03-30 18:44:55 +0100513 phylink_pcs_config(pl, force_restart, &link_state);
Russell King97fec512020-02-15 15:50:03 +0000514}
515
Russell King9525ae82017-07-25 15:03:13 +0100516static const char *phylink_pause_to_str(int pause)
517{
518 switch (pause & MLO_PAUSE_TXRX_MASK) {
519 case MLO_PAUSE_TX | MLO_PAUSE_RX:
520 return "rx/tx";
521 case MLO_PAUSE_TX:
522 return "tx";
523 case MLO_PAUSE_RX:
524 return "rx";
525 default:
526 return "off";
527 }
528}
529
Russell King4c0d6d32020-03-30 18:44:55 +0100530static void phylink_link_up(struct phylink *pl,
531 struct phylink_link_state link_state)
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300532{
533 struct net_device *ndev = pl->netdev;
534
David S. Millerb4b12b02019-05-31 10:49:43 -0700535 pl->cur_interface = link_state.interface;
Russell King4c0d6d32020-03-30 18:44:55 +0100536
537 if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
538 pl->pcs_ops->pcs_link_up(pl->config, pl->cur_link_an_mode,
539 pl->cur_interface,
540 link_state.speed, link_state.duplex);
541
Russell Kinge7765d62020-03-30 18:44:50 +0100542 pl->mac_ops->mac_link_up(pl->config, pl->phydev,
543 pl->cur_link_an_mode, pl->cur_interface,
544 link_state.speed, link_state.duplex,
545 !!(link_state.pause & MLO_PAUSE_TX),
546 !!(link_state.pause & MLO_PAUSE_RX));
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300547
Ioana Ciornei43de6192019-05-28 20:38:13 +0300548 if (ndev)
549 netif_carrier_on(ndev);
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300550
Ioana Ciornei170911802019-05-28 20:38:14 +0300551 phylink_info(pl,
552 "Link is Up - %s/%s - flow control %s\n",
553 phy_speed_to_str(link_state.speed),
554 phy_duplex_to_str(link_state.duplex),
555 phylink_pause_to_str(link_state.pause));
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300556}
557
Russell King4c0d6d32020-03-30 18:44:55 +0100558static void phylink_link_down(struct phylink *pl)
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300559{
560 struct net_device *ndev = pl->netdev;
561
Ioana Ciornei43de6192019-05-28 20:38:13 +0300562 if (ndev)
563 netif_carrier_off(ndev);
Russell Kinge7765d62020-03-30 18:44:50 +0100564 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
565 pl->cur_interface);
Ioana Ciornei170911802019-05-28 20:38:14 +0300566 phylink_info(pl, "Link is Down\n");
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300567}
568
Russell King9525ae82017-07-25 15:03:13 +0100569static void phylink_resolve(struct work_struct *w)
570{
571 struct phylink *pl = container_of(w, struct phylink, resolve);
572 struct phylink_link_state link_state;
573 struct net_device *ndev = pl->netdev;
Russell King319bfaf2020-07-21 12:03:55 +0100574 bool mac_config = false;
Russell Kingb06e5ca2020-07-21 12:03:50 +0100575 bool cur_link_state;
Russell King9525ae82017-07-25 15:03:13 +0100576
577 mutex_lock(&pl->state_mutex);
Russell Kingb06e5ca2020-07-21 12:03:50 +0100578 if (pl->netdev)
579 cur_link_state = netif_carrier_ok(ndev);
580 else
581 cur_link_state = pl->old_link_state;
582
Russell King9525ae82017-07-25 15:03:13 +0100583 if (pl->phylink_disable_state) {
584 pl->mac_link_dropped = false;
585 link_state.link = false;
586 } else if (pl->mac_link_dropped) {
587 link_state.link = false;
588 } else {
Russell King24cf0e62019-12-11 10:56:35 +0000589 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +0100590 case MLO_AN_PHY:
591 link_state = pl->phy_state;
Russell King2d5fbef2020-02-15 15:49:43 +0000592 phylink_apply_manual_flow(pl, &link_state);
Russell King319bfaf2020-07-21 12:03:55 +0100593 mac_config = link_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100594 break;
595
596 case MLO_AN_FIXED:
597 phylink_get_fixed_state(pl, &link_state);
Russell King319bfaf2020-07-21 12:03:55 +0100598 mac_config = link_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100599 break;
600
Russell King86a362c2017-12-01 10:24:26 +0000601 case MLO_AN_INBAND:
Russell Kingd46b7e42019-11-21 00:36:22 +0000602 phylink_mac_pcs_get_state(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100603
Russell King406cb0c2019-05-20 16:07:20 +0100604 /* If we have a phy, the "up" state is the union of
605 * both the PHY and the MAC */
606 if (pl->phydev)
607 link_state.link &= pl->phy_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100608
Russell King406cb0c2019-05-20 16:07:20 +0100609 /* Only update if the PHY link is up */
610 if (pl->phydev && pl->phy_state.link) {
611 link_state.interface = pl->phy_state.interface;
Russell King9525ae82017-07-25 15:03:13 +0100612
Russell King406cb0c2019-05-20 16:07:20 +0100613 /* If we have a PHY, we need to update with
Russell King33faac82020-02-15 15:49:48 +0000614 * the PHY flow control bits. */
615 link_state.pause = pl->phy_state.pause;
Russell King319bfaf2020-07-21 12:03:55 +0100616 mac_config = true;
Russell King9525ae82017-07-25 15:03:13 +0100617 }
Russell King319bfaf2020-07-21 12:03:55 +0100618 phylink_apply_manual_flow(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100619 break;
Russell King9525ae82017-07-25 15:03:13 +0100620 }
621 }
622
Russell King16319a72020-07-21 12:04:00 +0100623 if (mac_config) {
624 if (link_state.interface != pl->link_config.interface) {
625 /* The interface has changed, force the link down and
626 * then reconfigure.
627 */
628 if (cur_link_state) {
629 phylink_link_down(pl);
630 cur_link_state = false;
631 }
Russell King5005b162020-07-21 12:04:05 +0100632 phylink_pcs_config(pl, false, &link_state);
633 pl->link_config.interface = link_state.interface;
Russell King7cceb592020-07-21 12:04:10 +0100634 } else if (!pl->pcs_ops) {
Russell King5005b162020-07-21 12:04:05 +0100635 /* The interface remains unchanged, only the speed,
636 * duplex or pause settings have changed. Call the
Russell King7cceb592020-07-21 12:04:10 +0100637 * old mac_config() method to configure the MAC/PCS
638 * only if we do not have a PCS installed (an
639 * unconverted user.)
Russell King5005b162020-07-21 12:04:05 +0100640 */
641 phylink_mac_config(pl, &link_state);
Russell King16319a72020-07-21 12:04:00 +0100642 }
Russell King16319a72020-07-21 12:04:00 +0100643 }
Russell King319bfaf2020-07-21 12:03:55 +0100644
Russell Kingb06e5ca2020-07-21 12:03:50 +0100645 if (link_state.link != cur_link_state) {
Ioana Ciornei43de6192019-05-28 20:38:13 +0300646 pl->old_link_state = link_state.link;
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300647 if (!link_state.link)
Russell King4c0d6d32020-03-30 18:44:55 +0100648 phylink_link_down(pl);
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300649 else
Russell King4c0d6d32020-03-30 18:44:55 +0100650 phylink_link_up(pl, link_state);
Russell King9525ae82017-07-25 15:03:13 +0100651 }
652 if (!link_state.link && pl->mac_link_dropped) {
653 pl->mac_link_dropped = false;
654 queue_work(system_power_efficient_wq, &pl->resolve);
655 }
656 mutex_unlock(&pl->state_mutex);
657}
658
659static void phylink_run_resolve(struct phylink *pl)
660{
661 if (!pl->phylink_disable_state)
662 queue_work(system_power_efficient_wq, &pl->resolve);
663}
664
Russell King87454b62019-02-11 15:04:24 +0000665static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
666{
667 unsigned long state = pl->phylink_disable_state;
668
669 set_bit(bit, &pl->phylink_disable_state);
670 if (state == 0) {
671 queue_work(system_power_efficient_wq, &pl->resolve);
672 flush_work(&pl->resolve);
673 }
674}
675
Russell King9cd00a82018-05-10 13:17:31 -0700676static void phylink_fixed_poll(struct timer_list *t)
677{
678 struct phylink *pl = container_of(t, struct phylink, link_poll);
679
680 mod_timer(t, jiffies + HZ);
681
682 phylink_run_resolve(pl);
683}
684
Russell Kingce0aa272017-07-25 15:03:18 +0100685static const struct sfp_upstream_ops sfp_phylink_ops;
686
Russell King8fa7b9b2017-12-01 10:25:09 +0000687static int phylink_register_sfp(struct phylink *pl,
688 struct fwnode_handle *fwnode)
Russell Kingce0aa272017-07-25 15:03:18 +0100689{
Russell King2203cbf2019-10-15 11:38:39 +0100690 struct sfp_bus *bus;
Russell King8fa7b9b2017-12-01 10:25:09 +0000691 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100692
Russell Kingeed70fd2020-01-03 15:13:56 +0000693 if (!fwnode)
694 return 0;
695
Russell King727b3662019-11-08 17:39:29 +0000696 bus = sfp_bus_find_fwnode(fwnode);
Russell King2203cbf2019-10-15 11:38:39 +0100697 if (IS_ERR(bus)) {
698 ret = PTR_ERR(bus);
699 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
Russell King8fa7b9b2017-12-01 10:25:09 +0000700 return ret;
701 }
702
Russell King2203cbf2019-10-15 11:38:39 +0100703 pl->sfp_bus = bus;
Russell Kingce0aa272017-07-25 15:03:18 +0100704
Russell King727b3662019-11-08 17:39:29 +0000705 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
706 sfp_bus_put(bus);
707
708 return ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100709}
710
Russell King8796c892017-12-01 10:24:47 +0000711/**
712 * phylink_create() - create a phylink instance
Randy Dunlap9db74e52019-10-08 08:39:04 -0700713 * @config: a pointer to the target &struct phylink_config
Russell King8fa7b9b2017-12-01 10:25:09 +0000714 * @fwnode: a pointer to a &struct fwnode_handle describing the network
715 * interface
Russell King8796c892017-12-01 10:24:47 +0000716 * @iface: the desired link mode defined by &typedef phy_interface_t
Russell Kinge7765d62020-03-30 18:44:50 +0100717 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
Russell King8796c892017-12-01 10:24:47 +0000718 *
719 * Create a new phylink instance, and parse the link parameters found in @np.
720 * This will parse in-band modes, fixed-link or SFP configuration.
721 *
Russell King269a6b52019-11-19 17:18:52 +0000722 * Note: the rtnl lock must not be held when calling this function.
723 *
Russell King8796c892017-12-01 10:24:47 +0000724 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
725 * must use IS_ERR() to check for errors from this function.
726 */
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300727struct phylink *phylink_create(struct phylink_config *config,
Russell King8fa7b9b2017-12-01 10:25:09 +0000728 struct fwnode_handle *fwnode,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700729 phy_interface_t iface,
Russell Kinge7765d62020-03-30 18:44:50 +0100730 const struct phylink_mac_ops *mac_ops)
Russell King9525ae82017-07-25 15:03:13 +0100731{
732 struct phylink *pl;
733 int ret;
734
735 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
736 if (!pl)
737 return ERR_PTR(-ENOMEM);
738
739 mutex_init(&pl->state_mutex);
740 INIT_WORK(&pl->resolve, phylink_resolve);
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300741
742 pl->config = config;
743 if (config->type == PHYLINK_NETDEV) {
744 pl->netdev = to_net_dev(config->dev);
Ioana Ciornei43de6192019-05-28 20:38:13 +0300745 } else if (config->type == PHYLINK_DEV) {
746 pl->dev = config->dev;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300747 } else {
748 kfree(pl);
749 return ERR_PTR(-EINVAL);
750 }
751
Russell King9525ae82017-07-25 15:03:13 +0100752 pl->phy_state.interface = iface;
753 pl->link_interface = iface;
Florian Fainelli4be11ef2017-12-12 16:00:29 -0800754 if (iface == PHY_INTERFACE_MODE_MOCA)
755 pl->link_port = PORT_BNC;
756 else
757 pl->link_port = PORT_MII;
Russell King9525ae82017-07-25 15:03:13 +0100758 pl->link_config.interface = iface;
759 pl->link_config.pause = MLO_PAUSE_AN;
760 pl->link_config.speed = SPEED_UNKNOWN;
761 pl->link_config.duplex = DUPLEX_UNKNOWN;
Russell King74ee0e82017-12-20 23:21:34 +0000762 pl->link_config.an_enabled = true;
Russell Kinge7765d62020-03-30 18:44:50 +0100763 pl->mac_ops = mac_ops;
Russell King9525ae82017-07-25 15:03:13 +0100764 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King9cd00a82018-05-10 13:17:31 -0700765 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
Russell King9525ae82017-07-25 15:03:13 +0100766
767 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
768 linkmode_copy(pl->link_config.advertising, pl->supported);
769 phylink_validate(pl, pl->supported, &pl->link_config);
770
Russell King8fa7b9b2017-12-01 10:25:09 +0000771 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100772 if (ret < 0) {
773 kfree(pl);
774 return ERR_PTR(ret);
775 }
776
Russell King24cf0e62019-12-11 10:56:35 +0000777 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000778 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100779 if (ret < 0) {
780 kfree(pl);
781 return ERR_PTR(ret);
782 }
783 }
784
Russell King24cf0e62019-12-11 10:56:35 +0000785 pl->cur_link_an_mode = pl->cfg_link_an_mode;
786
Russell King8fa7b9b2017-12-01 10:25:09 +0000787 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100788 if (ret < 0) {
789 kfree(pl);
790 return ERR_PTR(ret);
791 }
792
Russell King9525ae82017-07-25 15:03:13 +0100793 return pl;
794}
795EXPORT_SYMBOL_GPL(phylink_create);
796
Russell King4c0d6d32020-03-30 18:44:55 +0100797void phylink_add_pcs(struct phylink *pl, const struct phylink_pcs_ops *ops)
798{
799 pl->pcs_ops = ops;
800}
801EXPORT_SYMBOL_GPL(phylink_add_pcs);
802
Russell King8796c892017-12-01 10:24:47 +0000803/**
804 * phylink_destroy() - cleanup and destroy the phylink instance
805 * @pl: a pointer to a &struct phylink returned from phylink_create()
806 *
807 * Destroy a phylink instance. Any PHY that has been attached must have been
808 * cleaned up via phylink_disconnect_phy() prior to calling this function.
Russell King269a6b52019-11-19 17:18:52 +0000809 *
810 * Note: the rtnl lock must not be held when calling this function.
Russell King8796c892017-12-01 10:24:47 +0000811 */
Russell King9525ae82017-07-25 15:03:13 +0100812void phylink_destroy(struct phylink *pl)
813{
Russell King727b3662019-11-08 17:39:29 +0000814 sfp_bus_del_upstream(pl->sfp_bus);
Russell King7b3b0e82019-05-28 10:57:23 +0100815 if (pl->link_gpio)
Florian Fainellidaab3342018-05-10 13:17:30 -0700816 gpiod_put(pl->link_gpio);
Russell Kingce0aa272017-07-25 15:03:18 +0100817
Russell King9525ae82017-07-25 15:03:13 +0100818 cancel_work_sync(&pl->resolve);
819 kfree(pl);
820}
821EXPORT_SYMBOL_GPL(phylink_destroy);
822
Doug Bergera3075932020-05-18 15:23:59 -0700823static void phylink_phy_change(struct phy_device *phydev, bool up)
Russell King9525ae82017-07-25 15:03:13 +0100824{
825 struct phylink *pl = phydev->phylink;
Russell King33faac82020-02-15 15:49:48 +0000826 bool tx_pause, rx_pause;
827
828 phy_get_pause(phydev, &tx_pause, &rx_pause);
Russell King9525ae82017-07-25 15:03:13 +0100829
830 mutex_lock(&pl->state_mutex);
831 pl->phy_state.speed = phydev->speed;
832 pl->phy_state.duplex = phydev->duplex;
833 pl->phy_state.pause = MLO_PAUSE_NONE;
Russell King33faac82020-02-15 15:49:48 +0000834 if (tx_pause)
835 pl->phy_state.pause |= MLO_PAUSE_TX;
836 if (rx_pause)
837 pl->phy_state.pause |= MLO_PAUSE_RX;
Russell King9525ae82017-07-25 15:03:13 +0100838 pl->phy_state.interface = phydev->interface;
839 pl->phy_state.link = up;
840 mutex_unlock(&pl->state_mutex);
841
842 phylink_run_resolve(pl);
843
Ioana Ciornei170911802019-05-28 20:38:14 +0300844 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
845 phy_modes(phydev->interface),
846 phy_speed_to_str(phydev->speed),
847 phy_duplex_to_str(phydev->duplex));
Russell King9525ae82017-07-25 15:03:13 +0100848}
849
Russell Kinge45d1f52019-12-11 10:56:30 +0000850static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
851 phy_interface_t interface)
Russell King9525ae82017-07-25 15:03:13 +0100852{
853 struct phylink_link_state config;
854 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
Florian Fainellie27f1782020-01-12 09:35:38 -0800855 char *irq_str;
Russell King9525ae82017-07-25 15:03:13 +0100856 int ret;
857
Russell King9525ae82017-07-25 15:03:13 +0100858 /*
859 * This is the new way of dealing with flow control for PHYs,
860 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
861 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
862 * using our validate call to the MAC, we rely upon the MAC
863 * clearing the bits from both supported and advertising fields.
864 */
Russell King725ea4b2019-11-15 20:05:45 +0000865 phy_support_asym_pause(phy);
866
867 memset(&config, 0, sizeof(config));
868 linkmode_copy(supported, phy->supported);
869 linkmode_copy(config.advertising, phy->advertising);
Russell Kingdf3f57a2019-12-13 18:22:07 +0000870
871 /* Clause 45 PHYs switch their Serdes lane between several different
872 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
873 * speeds. We really need to know which interface modes the PHY and
874 * MAC supports to properly work out which linkmodes can be supported.
875 */
876 if (phy->is_c45 &&
877 interface != PHY_INTERFACE_MODE_RXAUI &&
878 interface != PHY_INTERFACE_MODE_XAUI &&
879 interface != PHY_INTERFACE_MODE_USXGMII)
880 config.interface = PHY_INTERFACE_MODE_NA;
881 else
882 config.interface = interface;
Russell King9525ae82017-07-25 15:03:13 +0100883
884 ret = phylink_validate(pl, supported, &config);
Hauke Mehrtens20d8bb02020-03-02 00:55:02 +0100885 if (ret) {
886 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
887 phy_modes(config.interface),
888 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
889 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
890 ret);
Russell King9525ae82017-07-25 15:03:13 +0100891 return ret;
Hauke Mehrtens20d8bb02020-03-02 00:55:02 +0100892 }
Russell King9525ae82017-07-25 15:03:13 +0100893
894 phy->phylink = pl;
895 phy->phy_link_change = phylink_phy_change;
896
Florian Fainellie27f1782020-01-12 09:35:38 -0800897 irq_str = phy_attached_info_irq(phy);
Ioana Ciornei170911802019-05-28 20:38:14 +0300898 phylink_info(pl,
Florian Fainellie27f1782020-01-12 09:35:38 -0800899 "PHY [%s] driver [%s] (irq=%s)\n",
900 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
901 kfree(irq_str);
Russell King9525ae82017-07-25 15:03:13 +0100902
903 mutex_lock(&phy->lock);
904 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100905 pl->phydev = phy;
Russell Kinge45d1f52019-12-11 10:56:30 +0000906 pl->phy_state.interface = interface;
Russell King97fec512020-02-15 15:50:03 +0000907 pl->phy_state.pause = MLO_PAUSE_NONE;
908 pl->phy_state.speed = SPEED_UNKNOWN;
909 pl->phy_state.duplex = DUPLEX_UNKNOWN;
Russell King9525ae82017-07-25 15:03:13 +0100910 linkmode_copy(pl->supported, supported);
911 linkmode_copy(pl->link_config.advertising, config.advertising);
912
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000913 /* Restrict the phy advertisement according to the MAC support. */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100914 linkmode_copy(phy->advertising, config.advertising);
Russell King9525ae82017-07-25 15:03:13 +0100915 mutex_unlock(&pl->state_mutex);
916 mutex_unlock(&phy->lock);
917
Ioana Ciornei170911802019-05-28 20:38:14 +0300918 phylink_dbg(pl,
919 "phy: setting supported %*pb advertising %*pb\n",
920 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
921 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100922
Heiner Kallweit434a4312019-01-23 07:31:58 +0100923 if (phy_interrupt_is_valid(phy))
924 phy_request_interrupt(phy);
Russell King9525ae82017-07-25 15:03:13 +0100925
926 return 0;
927}
928
Russell King938d44c2019-12-11 10:56:25 +0000929static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
930 phy_interface_t interface)
Baruch Siach7e418372018-10-03 19:04:49 +0300931{
Russell King24cf0e62019-12-11 10:56:35 +0000932 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
933 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
Baruch Siach7e418372018-10-03 19:04:49 +0300934 phy_interface_mode_is_8023z(interface))))
935 return -EINVAL;
936
937 if (pl->phydev)
938 return -EBUSY;
939
Russell King938d44c2019-12-11 10:56:25 +0000940 return phy_attach_direct(pl->netdev, phy, 0, interface);
Baruch Siach7e418372018-10-03 19:04:49 +0300941}
942
Russell King8796c892017-12-01 10:24:47 +0000943/**
944 * phylink_connect_phy() - connect a PHY to the phylink instance
945 * @pl: a pointer to a &struct phylink returned from phylink_create()
946 * @phy: a pointer to a &struct phy_device.
947 *
948 * Connect @phy to the phylink instance specified by @pl by calling
949 * phy_attach_direct(). Configure the @phy according to the MAC driver's
950 * capabilities, start the PHYLIB state machine and enable any interrupts
951 * that the PHY supports.
952 *
953 * This updates the phylink's ethtool supported and advertising link mode
954 * masks.
955 *
956 * Returns 0 on success or a negative errno.
957 */
Russell King9525ae82017-07-25 15:03:13 +0100958int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
959{
Russell King938d44c2019-12-11 10:56:25 +0000960 int ret;
961
Florian Fainelli4904b6e2017-12-12 16:00:26 -0800962 /* Use PHY device/driver interface */
963 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
964 pl->link_interface = phy->interface;
965 pl->link_config.interface = pl->link_interface;
966 }
967
Russell King938d44c2019-12-11 10:56:25 +0000968 ret = phylink_attach_phy(pl, phy, pl->link_interface);
969 if (ret < 0)
970 return ret;
971
Russell Kinge45d1f52019-12-11 10:56:30 +0000972 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
Russell King938d44c2019-12-11 10:56:25 +0000973 if (ret)
974 phy_detach(phy);
975
976 return ret;
Russell King9525ae82017-07-25 15:03:13 +0100977}
978EXPORT_SYMBOL_GPL(phylink_connect_phy);
979
Russell King8796c892017-12-01 10:24:47 +0000980/**
981 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
982 * @pl: a pointer to a &struct phylink returned from phylink_create()
983 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -0800984 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +0000985 *
986 * Connect the phy specified in the device node @dn to the phylink instance
987 * specified by @pl. Actions specified in phylink_connect_phy() will be
988 * performed.
989 *
990 * Returns 0 on success or a negative errno.
991 */
Florian Fainelli0a629642017-12-12 16:00:25 -0800992int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
993 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +0100994{
995 struct device_node *phy_node;
996 struct phy_device *phy_dev;
997 int ret;
998
Russell Kingcf4f2672017-12-01 10:24:21 +0000999 /* Fixed links and 802.3z are handled without needing a PHY */
Russell King24cf0e62019-12-11 10:56:35 +00001000 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1001 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
Russell King86a362c2017-12-01 10:24:26 +00001002 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +01001003 return 0;
1004
1005 phy_node = of_parse_phandle(dn, "phy-handle", 0);
1006 if (!phy_node)
1007 phy_node = of_parse_phandle(dn, "phy", 0);
1008 if (!phy_node)
1009 phy_node = of_parse_phandle(dn, "phy-device", 0);
1010
1011 if (!phy_node) {
Russell King24cf0e62019-12-11 10:56:35 +00001012 if (pl->cfg_link_an_mode == MLO_AN_PHY)
Russell King9525ae82017-07-25 15:03:13 +01001013 return -ENODEV;
Russell King9525ae82017-07-25 15:03:13 +01001014 return 0;
1015 }
1016
Russell Kingf5058a22019-12-12 17:16:12 +00001017 phy_dev = of_phy_find_device(phy_node);
Russell King9525ae82017-07-25 15:03:13 +01001018 /* We're done with the phy_node handle */
1019 of_node_put(phy_node);
Russell King9525ae82017-07-25 15:03:13 +01001020 if (!phy_dev)
1021 return -ENODEV;
1022
Russell Kingf5058a22019-12-12 17:16:12 +00001023 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1024 pl->link_interface);
1025 if (ret)
1026 return ret;
1027
Russell Kinge45d1f52019-12-11 10:56:30 +00001028 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
Russell King9525ae82017-07-25 15:03:13 +01001029 if (ret)
1030 phy_detach(phy_dev);
1031
1032 return ret;
1033}
1034EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1035
Russell King8796c892017-12-01 10:24:47 +00001036/**
1037 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1038 * instance.
1039 * @pl: a pointer to a &struct phylink returned from phylink_create()
1040 *
1041 * Disconnect any current PHY from the phylink instance described by @pl.
1042 */
Russell King9525ae82017-07-25 15:03:13 +01001043void phylink_disconnect_phy(struct phylink *pl)
1044{
1045 struct phy_device *phy;
1046
Russell King8b874512017-12-15 16:09:47 +00001047 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001048
1049 phy = pl->phydev;
1050 if (phy) {
1051 mutex_lock(&phy->lock);
1052 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +01001053 pl->phydev = NULL;
1054 mutex_unlock(&pl->state_mutex);
1055 mutex_unlock(&phy->lock);
1056 flush_work(&pl->resolve);
1057
1058 phy_disconnect(phy);
1059 }
1060}
1061EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1062
Russell King8796c892017-12-01 10:24:47 +00001063/**
1064 * phylink_mac_change() - notify phylink of a change in MAC state
1065 * @pl: a pointer to a &struct phylink returned from phylink_create()
1066 * @up: indicates whether the link is currently up.
1067 *
1068 * The MAC driver should call this driver when the state of its link
1069 * changes (eg, link failure, new negotiation results, etc.)
1070 */
Russell King9525ae82017-07-25 15:03:13 +01001071void phylink_mac_change(struct phylink *pl, bool up)
1072{
1073 if (!up)
1074 pl->mac_link_dropped = true;
1075 phylink_run_resolve(pl);
Ioana Ciornei170911802019-05-28 20:38:14 +03001076 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
Russell King9525ae82017-07-25 15:03:13 +01001077}
1078EXPORT_SYMBOL_GPL(phylink_mac_change);
1079
Russell King7b3b0e82019-05-28 10:57:23 +01001080static irqreturn_t phylink_link_handler(int irq, void *data)
1081{
1082 struct phylink *pl = data;
1083
1084 phylink_run_resolve(pl);
1085
1086 return IRQ_HANDLED;
1087}
1088
Russell King8796c892017-12-01 10:24:47 +00001089/**
1090 * phylink_start() - start a phylink instance
1091 * @pl: a pointer to a &struct phylink returned from phylink_create()
1092 *
1093 * Start the phylink instance specified by @pl, configuring the MAC for the
1094 * desired link mode(s) and negotiation style. This should be called from the
1095 * network device driver's &struct net_device_ops ndo_open() method.
1096 */
Russell King9525ae82017-07-25 15:03:13 +01001097void phylink_start(struct phylink *pl)
1098{
Russell King5c05c1d2020-04-23 17:02:56 +01001099 bool poll = false;
1100
Russell King8b874512017-12-15 16:09:47 +00001101 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001102
Ioana Ciornei170911802019-05-28 20:38:14 +03001103 phylink_info(pl, "configuring for %s/%s link mode\n",
Russell King24cf0e62019-12-11 10:56:35 +00001104 phylink_an_mode_str(pl->cur_link_an_mode),
Ioana Ciornei170911802019-05-28 20:38:14 +03001105 phy_modes(pl->link_config.interface));
Russell King9525ae82017-07-25 15:03:13 +01001106
Antoine Tenartaeeb2e82018-09-19 11:39:31 +02001107 /* Always set the carrier off */
Ioana Ciornei43de6192019-05-28 20:38:13 +03001108 if (pl->netdev)
1109 netif_carrier_off(pl->netdev);
Antoine Tenartaeeb2e82018-09-19 11:39:31 +02001110
Russell King9525ae82017-07-25 15:03:13 +01001111 /* Apply the link configuration to the MAC when starting. This allows
1112 * a fixed-link to start with the correct parameters, and also
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001113 * ensures that we set the appropriate advertisement for Serdes links.
Russell King4c0d6d32020-03-30 18:44:55 +01001114 *
1115 * Restart autonegotiation if using 802.3z to ensure that the link
Russell King85b43942017-12-01 10:24:42 +00001116 * parameters are properly negotiated. This is necessary for DSA
1117 * switches using 802.3z negotiation to ensure they see our modes.
1118 */
Russell King4c0d6d32020-03-30 18:44:55 +01001119 phylink_mac_initial_config(pl, true);
Russell King85b43942017-12-01 10:24:42 +00001120
Russell King9525ae82017-07-25 15:03:13 +01001121 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1122 phylink_run_resolve(pl);
1123
Russell King24cf0e62019-12-11 10:56:35 +00001124 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
Russell King7b3b0e82019-05-28 10:57:23 +01001125 int irq = gpiod_to_irq(pl->link_gpio);
1126
1127 if (irq > 0) {
1128 if (!request_irq(irq, phylink_link_handler,
1129 IRQF_TRIGGER_RISING |
1130 IRQF_TRIGGER_FALLING,
1131 "netdev link", pl))
1132 pl->link_irq = irq;
1133 else
1134 irq = 0;
1135 }
1136 if (irq <= 0)
Russell King5c05c1d2020-04-23 17:02:56 +01001137 poll = true;
Russell King7b3b0e82019-05-28 10:57:23 +01001138 }
Russell King5c05c1d2020-04-23 17:02:56 +01001139
1140 switch (pl->cfg_link_an_mode) {
1141 case MLO_AN_FIXED:
1142 poll |= pl->config->poll_fixed_state;
1143 break;
1144 case MLO_AN_INBAND:
1145 poll |= pl->config->pcs_poll;
1146 break;
1147 }
1148 if (poll)
Russell King9cd00a82018-05-10 13:17:31 -07001149 mod_timer(&pl->link_poll, jiffies + HZ);
Russell King9525ae82017-07-25 15:03:13 +01001150 if (pl->phydev)
1151 phy_start(pl->phydev);
Arseny Solokhac7fa7f52019-07-24 20:31:39 +07001152 if (pl->sfp_bus)
1153 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +01001154}
1155EXPORT_SYMBOL_GPL(phylink_start);
1156
Russell King8796c892017-12-01 10:24:47 +00001157/**
1158 * phylink_stop() - stop a phylink instance
1159 * @pl: a pointer to a &struct phylink returned from phylink_create()
1160 *
1161 * Stop the phylink instance specified by @pl. This should be called from the
1162 * network device driver's &struct net_device_ops ndo_stop() method. The
1163 * network device's carrier state should not be changed prior to calling this
1164 * function.
1165 */
Russell King9525ae82017-07-25 15:03:13 +01001166void phylink_stop(struct phylink *pl)
1167{
Russell King8b874512017-12-15 16:09:47 +00001168 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001169
Russell Kingce0aa272017-07-25 15:03:18 +01001170 if (pl->sfp_bus)
1171 sfp_upstream_stop(pl->sfp_bus);
Arseny Solokhac7fa7f52019-07-24 20:31:39 +07001172 if (pl->phydev)
1173 phy_stop(pl->phydev);
Russell King7b3b0e82019-05-28 10:57:23 +01001174 del_timer_sync(&pl->link_poll);
1175 if (pl->link_irq) {
1176 free_irq(pl->link_irq, pl);
1177 pl->link_irq = 0;
1178 }
Russell King9525ae82017-07-25 15:03:13 +01001179
Russell King87454b62019-02-11 15:04:24 +00001180 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
Russell King9525ae82017-07-25 15:03:13 +01001181}
1182EXPORT_SYMBOL_GPL(phylink_stop);
1183
Russell King8796c892017-12-01 10:24:47 +00001184/**
1185 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1186 * @pl: a pointer to a &struct phylink returned from phylink_create()
1187 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1188 *
1189 * Read the wake on lan parameters from the PHY attached to the phylink
1190 * instance specified by @pl. If no PHY is currently attached, report no
1191 * support for wake on lan.
1192 */
Russell King9525ae82017-07-25 15:03:13 +01001193void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1194{
Russell King8b874512017-12-15 16:09:47 +00001195 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001196
1197 wol->supported = 0;
1198 wol->wolopts = 0;
1199
1200 if (pl->phydev)
1201 phy_ethtool_get_wol(pl->phydev, wol);
1202}
1203EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1204
Russell King8796c892017-12-01 10:24:47 +00001205/**
1206 * phylink_ethtool_set_wol() - set wake on lan parameters
1207 * @pl: a pointer to a &struct phylink returned from phylink_create()
1208 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1209 *
1210 * Set the wake on lan parameters for the PHY attached to the phylink
1211 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1212 * error.
1213 *
1214 * Returns zero on success or negative errno code.
1215 */
Russell King9525ae82017-07-25 15:03:13 +01001216int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1217{
1218 int ret = -EOPNOTSUPP;
1219
Russell King8b874512017-12-15 16:09:47 +00001220 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001221
1222 if (pl->phydev)
1223 ret = phy_ethtool_set_wol(pl->phydev, wol);
1224
1225 return ret;
1226}
1227EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1228
1229static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1230{
1231 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1232
1233 linkmode_zero(mask);
1234 phylink_set_port_modes(mask);
1235
1236 linkmode_and(dst, dst, mask);
1237 linkmode_or(dst, dst, b);
1238}
1239
1240static void phylink_get_ksettings(const struct phylink_link_state *state,
1241 struct ethtool_link_ksettings *kset)
1242{
1243 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1244 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1245 kset->base.speed = state->speed;
1246 kset->base.duplex = state->duplex;
1247 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1248 AUTONEG_DISABLE;
1249}
1250
Russell King8796c892017-12-01 10:24:47 +00001251/**
1252 * phylink_ethtool_ksettings_get() - get the current link settings
1253 * @pl: a pointer to a &struct phylink returned from phylink_create()
1254 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1255 *
1256 * Read the current link settings for the phylink instance specified by @pl.
1257 * This will be the link settings read from the MAC, PHY or fixed link
1258 * settings depending on the current negotiation mode.
1259 */
Russell King9525ae82017-07-25 15:03:13 +01001260int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001261 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001262{
1263 struct phylink_link_state link_state;
1264
Russell King8b874512017-12-15 16:09:47 +00001265 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001266
1267 if (pl->phydev) {
1268 phy_ethtool_ksettings_get(pl->phydev, kset);
1269 } else {
1270 kset->base.port = pl->link_port;
1271 }
1272
1273 linkmode_copy(kset->link_modes.supported, pl->supported);
1274
Russell King24cf0e62019-12-11 10:56:35 +00001275 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +01001276 case MLO_AN_FIXED:
1277 /* We are using fixed settings. Report these as the
1278 * current link settings - and note that these also
1279 * represent the supported speeds/duplex/pause modes.
1280 */
1281 phylink_get_fixed_state(pl, &link_state);
1282 phylink_get_ksettings(&link_state, kset);
1283 break;
1284
Russell King86a362c2017-12-01 10:24:26 +00001285 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001286 /* If there is a phy attached, then use the reported
1287 * settings from the phy with no modification.
1288 */
1289 if (pl->phydev)
1290 break;
1291
Russell Kingd46b7e42019-11-21 00:36:22 +00001292 phylink_mac_pcs_get_state(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +01001293
1294 /* The MAC is reporting the link results from its own PCS
1295 * layer via in-band status. Report these as the current
1296 * link settings.
1297 */
1298 phylink_get_ksettings(&link_state, kset);
1299 break;
1300 }
1301
1302 return 0;
1303}
1304EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1305
Russell King8796c892017-12-01 10:24:47 +00001306/**
1307 * phylink_ethtool_ksettings_set() - set the link settings
1308 * @pl: a pointer to a &struct phylink returned from phylink_create()
1309 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1310 */
Russell King9525ae82017-07-25 15:03:13 +01001311int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001312 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001313{
Russell King77316762019-06-02 15:12:54 +01001314 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
Russell King9525ae82017-07-25 15:03:13 +01001315 struct phylink_link_state config;
Russell Kingc8cab712020-07-21 12:04:16 +01001316 const struct phy_setting *s;
Russell King9525ae82017-07-25 15:03:13 +01001317
Russell King8b874512017-12-15 16:09:47 +00001318 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001319
Russell Kingcbc1bb12020-07-21 12:04:21 +01001320 if (pl->phydev) {
1321 /* We can rely on phylib for this update; we also do not need
1322 * to update the pl->link_config settings:
1323 * - the configuration returned via ksettings_get() will come
1324 * from phylib whenever a PHY is present.
1325 * - link_config.interface will be updated by the PHY calling
1326 * back via phylink_phy_change() and a subsequent resolve.
1327 * - initial link configuration for PHY mode comes from the
1328 * last phy state updated via phylink_phy_change().
1329 * - other configuration changes (e.g. pause modes) are
1330 * performed directly via phylib.
1331 * - if in in-band mode with a PHY, the link configuration
1332 * is passed on the link from the PHY, and all of
1333 * link_config.{speed,duplex,an_enabled,pause} are not used.
1334 * - the only possible use would be link_config.advertising
1335 * pause modes when in 1000base-X mode with a PHY, but in
1336 * the presence of a PHY, this should not be changed as that
1337 * should be determined from the media side advertisement.
1338 */
1339 return phy_ethtool_ksettings_set(pl->phydev, kset);
1340 }
1341
Russell King77316762019-06-02 15:12:54 +01001342 linkmode_copy(support, pl->supported);
Russell King9525ae82017-07-25 15:03:13 +01001343 config = pl->link_config;
Russell Kingc8cab712020-07-21 12:04:16 +01001344 config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
Russell King9525ae82017-07-25 15:03:13 +01001345
Russell Kingc8cab712020-07-21 12:04:16 +01001346 /* Mask out unsupported advertisements, and force the autoneg bit */
Russell King9525ae82017-07-25 15:03:13 +01001347 linkmode_and(config.advertising, kset->link_modes.advertising,
Russell King77316762019-06-02 15:12:54 +01001348 support);
Russell Kingc8cab712020-07-21 12:04:16 +01001349 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
1350 config.an_enabled);
Russell King9525ae82017-07-25 15:03:13 +01001351
1352 /* FIXME: should we reject autoneg if phy/mac does not support it? */
Russell Kingc8cab712020-07-21 12:04:16 +01001353 switch (kset->base.autoneg) {
1354 case AUTONEG_DISABLE:
Russell King9525ae82017-07-25 15:03:13 +01001355 /* Autonegotiation disabled, select a suitable speed and
1356 * duplex.
1357 */
1358 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
Russell King77316762019-06-02 15:12:54 +01001359 support, false);
Russell King9525ae82017-07-25 15:03:13 +01001360 if (!s)
1361 return -EINVAL;
1362
1363 /* If we have a fixed link (as specified by firmware), refuse
1364 * to change link parameters.
1365 */
Russell King24cf0e62019-12-11 10:56:35 +00001366 if (pl->cur_link_an_mode == MLO_AN_FIXED &&
Russell King9525ae82017-07-25 15:03:13 +01001367 (s->speed != pl->link_config.speed ||
1368 s->duplex != pl->link_config.duplex))
1369 return -EINVAL;
1370
1371 config.speed = s->speed;
1372 config.duplex = s->duplex;
Russell Kingc8cab712020-07-21 12:04:16 +01001373 break;
Russell King9525ae82017-07-25 15:03:13 +01001374
Russell Kingc8cab712020-07-21 12:04:16 +01001375 case AUTONEG_ENABLE:
Russell King9525ae82017-07-25 15:03:13 +01001376 /* If we have a fixed link, refuse to enable autonegotiation */
Russell King24cf0e62019-12-11 10:56:35 +00001377 if (pl->cur_link_an_mode == MLO_AN_FIXED)
Russell King9525ae82017-07-25 15:03:13 +01001378 return -EINVAL;
1379
1380 config.speed = SPEED_UNKNOWN;
1381 config.duplex = DUPLEX_UNKNOWN;
Russell Kingc8cab712020-07-21 12:04:16 +01001382 break;
Russell King9525ae82017-07-25 15:03:13 +01001383
Russell Kingc8cab712020-07-21 12:04:16 +01001384 default:
1385 return -EINVAL;
Russell King9525ae82017-07-25 15:03:13 +01001386 }
1387
Russell Kingcbc1bb12020-07-21 12:04:21 +01001388 /* For a fixed link, this isn't able to change any parameters,
1389 * which just leaves inband mode.
1390 */
1391 if (phylink_validate(pl, support, &config))
1392 return -EINVAL;
1393
1394 /* If autonegotiation is enabled, we must have an advertisement */
1395 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1396 return -EINVAL;
1397
1398 mutex_lock(&pl->state_mutex);
1399 linkmode_copy(pl->link_config.advertising, config.advertising);
1400 pl->link_config.interface = config.interface;
1401 pl->link_config.speed = config.speed;
1402 pl->link_config.duplex = config.duplex;
Russell Kinga83c8822020-07-21 12:04:26 +01001403 pl->link_config.an_enabled = config.an_enabled;
Russell Kingcbc1bb12020-07-21 12:04:21 +01001404
1405 if (pl->cur_link_an_mode == MLO_AN_INBAND &&
1406 !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1407 /* If in 802.3z mode, this updates the advertisement.
1408 *
1409 * If we are in SGMII mode without a PHY, there is no
1410 * advertisement; the only thing we have is the pause
1411 * modes which can only come from a PHY.
Russell King5d57c322019-12-13 18:22:02 +00001412 */
Russell Kingcbc1bb12020-07-21 12:04:21 +01001413 phylink_pcs_config(pl, true, &pl->link_config);
Russell King9525ae82017-07-25 15:03:13 +01001414 }
Russell Kingcbc1bb12020-07-21 12:04:21 +01001415 mutex_unlock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +01001416
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001417 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001418}
1419EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1420
Russell King8796c892017-12-01 10:24:47 +00001421/**
1422 * phylink_ethtool_nway_reset() - restart negotiation
1423 * @pl: a pointer to a &struct phylink returned from phylink_create()
1424 *
1425 * Restart negotiation for the phylink instance specified by @pl. This will
1426 * cause any attached phy to restart negotiation with the link partner, and
1427 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1428 * negotiation.
1429 *
1430 * Returns zero on success, or negative error code.
1431 */
Russell King9525ae82017-07-25 15:03:13 +01001432int phylink_ethtool_nway_reset(struct phylink *pl)
1433{
1434 int ret = 0;
1435
Russell King8b874512017-12-15 16:09:47 +00001436 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001437
1438 if (pl->phydev)
1439 ret = phy_restart_aneg(pl->phydev);
Russell King4c0d6d32020-03-30 18:44:55 +01001440 phylink_mac_pcs_an_restart(pl);
Russell King9525ae82017-07-25 15:03:13 +01001441
1442 return ret;
1443}
1444EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1445
Russell King8796c892017-12-01 10:24:47 +00001446/**
1447 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1448 * @pl: a pointer to a &struct phylink returned from phylink_create()
1449 * @pause: a pointer to a &struct ethtool_pauseparam
1450 */
Russell King9525ae82017-07-25 15:03:13 +01001451void phylink_ethtool_get_pauseparam(struct phylink *pl,
1452 struct ethtool_pauseparam *pause)
1453{
Russell King8b874512017-12-15 16:09:47 +00001454 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001455
1456 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1457 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1458 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1459}
1460EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1461
Russell King8796c892017-12-01 10:24:47 +00001462/**
1463 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1464 * @pl: a pointer to a &struct phylink returned from phylink_create()
1465 * @pause: a pointer to a &struct ethtool_pauseparam
1466 */
Russell King9525ae82017-07-25 15:03:13 +01001467int phylink_ethtool_set_pauseparam(struct phylink *pl,
1468 struct ethtool_pauseparam *pause)
1469{
1470 struct phylink_link_state *config = &pl->link_config;
Russell King2e919bc2020-06-23 17:47:29 +01001471 bool manual_changed;
1472 int pause_state;
Russell King9525ae82017-07-25 15:03:13 +01001473
Russell King8b874512017-12-15 16:09:47 +00001474 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001475
Russell King8cdfa252020-02-15 15:49:37 +00001476 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1477 return -EOPNOTSUPP;
1478
Russell King9525ae82017-07-25 15:03:13 +01001479 if (!phylink_test(pl->supported, Pause) &&
1480 !phylink_test(pl->supported, Asym_Pause))
1481 return -EOPNOTSUPP;
1482
1483 if (!phylink_test(pl->supported, Asym_Pause) &&
1484 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1485 return -EINVAL;
1486
Russell King2e919bc2020-06-23 17:47:29 +01001487 pause_state = 0;
Russell King9525ae82017-07-25 15:03:13 +01001488 if (pause->autoneg)
Russell King2e919bc2020-06-23 17:47:29 +01001489 pause_state |= MLO_PAUSE_AN;
Russell King9525ae82017-07-25 15:03:13 +01001490 if (pause->rx_pause)
Russell King2e919bc2020-06-23 17:47:29 +01001491 pause_state |= MLO_PAUSE_RX;
Russell King9525ae82017-07-25 15:03:13 +01001492 if (pause->tx_pause)
Russell King2e919bc2020-06-23 17:47:29 +01001493 pause_state |= MLO_PAUSE_TX;
Russell King9525ae82017-07-25 15:03:13 +01001494
Russell King2e919bc2020-06-23 17:47:29 +01001495 mutex_lock(&pl->state_mutex);
Russell Kingf904f152020-02-15 15:49:58 +00001496 /*
1497 * See the comments for linkmode_set_pause(), wrt the deficiencies
1498 * with the current implementation. A solution to this issue would
1499 * be:
1500 * ethtool Local device
1501 * rx tx Pause AsymDir
1502 * 0 0 0 0
1503 * 1 0 1 1
1504 * 0 1 0 1
1505 * 1 1 1 1
1506 * and then use the ethtool rx/tx enablement status to mask the
1507 * rx/tx pause resolution.
1508 */
1509 linkmode_set_pause(config->advertising, pause->tx_pause,
1510 pause->rx_pause);
1511
Russell King2e919bc2020-06-23 17:47:29 +01001512 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1513 (!(pause_state & MLO_PAUSE_AN) &&
1514 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1515
1516 config->pause = pause_state;
1517
Russell Kingc718af22020-06-23 17:47:23 +01001518 if (!pl->phydev && !test_bit(PHYLINK_DISABLE_STOPPED,
1519 &pl->phylink_disable_state))
1520 phylink_pcs_config(pl, true, &pl->link_config);
1521
1522 mutex_unlock(&pl->state_mutex);
1523
1524 /* If we have a PHY, a change of the pause frame advertisement will
1525 * cause phylib to renegotiate (if AN is enabled) which will in turn
1526 * call our phylink_phy_change() and trigger a resolve. Note that
1527 * we can't hold our state mutex while calling phy_set_asym_pause().
Russell Kingd9922c02019-11-19 17:28:14 +00001528 */
Russell Kingc718af22020-06-23 17:47:23 +01001529 if (pl->phydev)
Russell Kingd9922c02019-11-19 17:28:14 +00001530 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1531 pause->tx_pause);
Russell King9525ae82017-07-25 15:03:13 +01001532
Russell King2e919bc2020-06-23 17:47:29 +01001533 /* If the manual pause settings changed, make sure we trigger a
1534 * resolve to update their state; we can not guarantee that the
1535 * link will cycle.
1536 */
1537 if (manual_changed) {
1538 pl->mac_link_dropped = true;
1539 phylink_run_resolve(pl);
Russell King9525ae82017-07-25 15:03:13 +01001540 }
Russell King9525ae82017-07-25 15:03:13 +01001541
1542 return 0;
1543}
1544EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1545
1546/**
1547 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1548 * counter
1549 * @pl: a pointer to a &struct phylink returned from phylink_create().
1550 *
1551 * Read the Energy Efficient Ethernet error counter from the PHY associated
1552 * with the phylink instance specified by @pl.
1553 *
1554 * Returns positive error counter value, or negative error code.
1555 */
1556int phylink_get_eee_err(struct phylink *pl)
1557{
1558 int ret = 0;
1559
1560 ASSERT_RTNL();
1561
1562 if (pl->phydev)
1563 ret = phy_get_eee_err(pl->phydev);
1564
1565 return ret;
1566}
1567EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1568
1569/**
Russell King86e58132019-02-11 11:46:06 +00001570 * phylink_init_eee() - init and check the EEE features
1571 * @pl: a pointer to a &struct phylink returned from phylink_create()
1572 * @clk_stop_enable: allow PHY to stop receive clock
1573 *
1574 * Must be called either with RTNL held or within mac_link_up()
1575 */
1576int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1577{
1578 int ret = -EOPNOTSUPP;
1579
1580 if (pl->phydev)
1581 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1582
1583 return ret;
1584}
1585EXPORT_SYMBOL_GPL(phylink_init_eee);
1586
1587/**
Russell King8796c892017-12-01 10:24:47 +00001588 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1589 * @pl: a pointer to a &struct phylink returned from phylink_create()
1590 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1591 */
Russell King9525ae82017-07-25 15:03:13 +01001592int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1593{
1594 int ret = -EOPNOTSUPP;
1595
Russell King8b874512017-12-15 16:09:47 +00001596 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001597
1598 if (pl->phydev)
1599 ret = phy_ethtool_get_eee(pl->phydev, eee);
1600
1601 return ret;
1602}
1603EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1604
Russell King8796c892017-12-01 10:24:47 +00001605/**
1606 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1607 * @pl: a pointer to a &struct phylink returned from phylink_create()
1608 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1609 */
Russell King9525ae82017-07-25 15:03:13 +01001610int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1611{
1612 int ret = -EOPNOTSUPP;
1613
Russell King8b874512017-12-15 16:09:47 +00001614 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001615
1616 if (pl->phydev)
1617 ret = phy_ethtool_set_eee(pl->phydev, eee);
1618
1619 return ret;
1620}
1621EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1622
1623/* This emulates MII registers for a fixed-mode phy operating as per the
1624 * passed in state. "aneg" defines if we report negotiation is possible.
1625 *
1626 * FIXME: should deal with negotiation state too.
1627 */
Russell King7fdc4552019-05-28 10:57:18 +01001628static int phylink_mii_emul_read(unsigned int reg,
1629 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +01001630{
1631 struct fixed_phy_status fs;
Russell King4e5aeb42020-02-15 15:49:53 +00001632 unsigned long *lpa = state->lp_advertising;
Russell King9525ae82017-07-25 15:03:13 +01001633 int val;
1634
1635 fs.link = state->link;
1636 fs.speed = state->speed;
1637 fs.duplex = state->duplex;
Russell King4e5aeb42020-02-15 15:49:53 +00001638 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1639 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
Russell King9525ae82017-07-25 15:03:13 +01001640
1641 val = swphy_read_reg(reg, &fs);
1642 if (reg == MII_BMSR) {
1643 if (!state->an_complete)
1644 val &= ~BMSR_ANEGCOMPLETE;
Russell King9525ae82017-07-25 15:03:13 +01001645 }
1646 return val;
1647}
1648
Russell Kingecbd87b2017-07-25 15:03:28 +01001649static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1650 unsigned int reg)
1651{
1652 struct phy_device *phydev = pl->phydev;
1653 int prtad, devad;
1654
1655 if (mdio_phy_id_is_c45(phy_id)) {
1656 prtad = mdio_phy_id_prtad(phy_id);
1657 devad = mdio_phy_id_devad(phy_id);
Russell King90ce6652020-05-26 16:29:36 +01001658 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001659 } else if (phydev->is_c45) {
1660 switch (reg) {
1661 case MII_BMCR:
1662 case MII_BMSR:
1663 case MII_PHYSID1:
1664 case MII_PHYSID2:
Russell King320ed3b2020-06-18 14:46:08 +01001665 devad = __ffs(phydev->c45_ids.mmds_present);
Russell Kingecbd87b2017-07-25 15:03:28 +01001666 break;
1667 case MII_ADVERTISE:
1668 case MII_LPA:
Russell King320ed3b2020-06-18 14:46:08 +01001669 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
Russell Kingecbd87b2017-07-25 15:03:28 +01001670 return -EINVAL;
1671 devad = MDIO_MMD_AN;
1672 if (reg == MII_ADVERTISE)
1673 reg = MDIO_AN_ADVERTISE;
1674 else
1675 reg = MDIO_AN_LPA;
1676 break;
1677 default:
1678 return -EINVAL;
1679 }
1680 prtad = phy_id;
Russell King90ce6652020-05-26 16:29:36 +01001681 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001682 } else {
1683 prtad = phy_id;
1684 devad = reg;
1685 }
1686 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1687}
1688
1689static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1690 unsigned int reg, unsigned int val)
1691{
1692 struct phy_device *phydev = pl->phydev;
1693 int prtad, devad;
1694
1695 if (mdio_phy_id_is_c45(phy_id)) {
1696 prtad = mdio_phy_id_prtad(phy_id);
1697 devad = mdio_phy_id_devad(phy_id);
Russell King90ce6652020-05-26 16:29:36 +01001698 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001699 } else if (phydev->is_c45) {
1700 switch (reg) {
1701 case MII_BMCR:
1702 case MII_BMSR:
1703 case MII_PHYSID1:
1704 case MII_PHYSID2:
Russell King320ed3b2020-06-18 14:46:08 +01001705 devad = __ffs(phydev->c45_ids.mmds_present);
Russell Kingecbd87b2017-07-25 15:03:28 +01001706 break;
1707 case MII_ADVERTISE:
1708 case MII_LPA:
Russell King320ed3b2020-06-18 14:46:08 +01001709 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
Russell Kingecbd87b2017-07-25 15:03:28 +01001710 return -EINVAL;
1711 devad = MDIO_MMD_AN;
1712 if (reg == MII_ADVERTISE)
1713 reg = MDIO_AN_ADVERTISE;
1714 else
1715 reg = MDIO_AN_LPA;
1716 break;
1717 default:
1718 return -EINVAL;
1719 }
1720 prtad = phy_id;
Russell King90ce6652020-05-26 16:29:36 +01001721 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001722 } else {
1723 prtad = phy_id;
1724 devad = reg;
1725 }
1726
1727 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1728}
1729
Russell King9525ae82017-07-25 15:03:13 +01001730static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1731 unsigned int reg)
1732{
1733 struct phylink_link_state state;
1734 int val = 0xffff;
1735
Russell King24cf0e62019-12-11 10:56:35 +00001736 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +01001737 case MLO_AN_FIXED:
1738 if (phy_id == 0) {
1739 phylink_get_fixed_state(pl, &state);
Russell King7fdc4552019-05-28 10:57:18 +01001740 val = phylink_mii_emul_read(reg, &state);
Russell King9525ae82017-07-25 15:03:13 +01001741 }
1742 break;
1743
1744 case MLO_AN_PHY:
1745 return -EOPNOTSUPP;
1746
Russell King86a362c2017-12-01 10:24:26 +00001747 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001748 if (phy_id == 0) {
Russell Kingd46b7e42019-11-21 00:36:22 +00001749 phylink_mac_pcs_get_state(pl, &state);
Russell King7fdc4552019-05-28 10:57:18 +01001750 val = phylink_mii_emul_read(reg, &state);
Russell King9525ae82017-07-25 15:03:13 +01001751 }
1752 break;
1753 }
1754
1755 return val & 0xffff;
1756}
1757
1758static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1759 unsigned int reg, unsigned int val)
1760{
Russell King24cf0e62019-12-11 10:56:35 +00001761 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +01001762 case MLO_AN_FIXED:
1763 break;
1764
1765 case MLO_AN_PHY:
1766 return -EOPNOTSUPP;
1767
Russell King86a362c2017-12-01 10:24:26 +00001768 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001769 break;
1770 }
1771
1772 return 0;
1773}
1774
Russell King8796c892017-12-01 10:24:47 +00001775/**
1776 * phylink_mii_ioctl() - generic mii ioctl interface
1777 * @pl: a pointer to a &struct phylink returned from phylink_create()
1778 * @ifr: a pointer to a &struct ifreq for socket ioctls
1779 * @cmd: ioctl cmd to execute
1780 *
1781 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1782 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1783 *
1784 * Returns: zero on success or negative error code.
1785 *
1786 * %SIOCGMIIPHY:
1787 * read register from the current PHY.
1788 * %SIOCGMIIREG:
1789 * read register from the specified PHY.
1790 * %SIOCSMIIREG:
1791 * set a register on the specified PHY.
1792 */
Russell King9525ae82017-07-25 15:03:13 +01001793int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1794{
Russell Kingecbd87b2017-07-25 15:03:28 +01001795 struct mii_ioctl_data *mii = if_mii(ifr);
1796 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001797
Russell King8b874512017-12-15 16:09:47 +00001798 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001799
Russell Kingecbd87b2017-07-25 15:03:28 +01001800 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001801 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001802 switch (cmd) {
1803 case SIOCGMIIPHY:
1804 mii->phy_id = pl->phydev->mdio.addr;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001805 /* fall through */
Russell King9525ae82017-07-25 15:03:13 +01001806
Russell Kingecbd87b2017-07-25 15:03:28 +01001807 case SIOCGMIIREG:
1808 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1809 if (ret >= 0) {
1810 mii->val_out = ret;
1811 ret = 0;
1812 }
1813 break;
Russell King9525ae82017-07-25 15:03:13 +01001814
Russell Kingecbd87b2017-07-25 15:03:28 +01001815 case SIOCSMIIREG:
1816 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1817 mii->val_in);
1818 break;
Russell King9525ae82017-07-25 15:03:13 +01001819
Russell Kingecbd87b2017-07-25 15:03:28 +01001820 default:
Russell King9525ae82017-07-25 15:03:13 +01001821 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001822 break;
1823 }
1824 } else {
1825 switch (cmd) {
1826 case SIOCGMIIPHY:
1827 mii->phy_id = 0;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001828 /* fall through */
Russell Kingecbd87b2017-07-25 15:03:28 +01001829
1830 case SIOCGMIIREG:
1831 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1832 if (ret >= 0) {
1833 mii->val_out = ret;
1834 ret = 0;
1835 }
1836 break;
1837
1838 case SIOCSMIIREG:
1839 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1840 mii->val_in);
1841 break;
1842
1843 default:
1844 ret = -EOPNOTSUPP;
1845 break;
1846 }
Russell King9525ae82017-07-25 15:03:13 +01001847 }
1848
1849 return ret;
1850}
1851EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1852
Russell Kingc6d5d842020-06-24 11:06:54 +01001853/**
1854 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
1855 * link partners
1856 * @pl: a pointer to a &struct phylink returned from phylink_create()
1857 * @sync: perform action synchronously
1858 *
1859 * If we have a PHY that is not part of a SFP module, then set the speed
1860 * as described in the phy_speed_down() function. Please see this function
1861 * for a description of the @sync parameter.
1862 *
1863 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
1864 */
1865int phylink_speed_down(struct phylink *pl, bool sync)
1866{
1867 int ret = 0;
1868
1869 ASSERT_RTNL();
1870
1871 if (!pl->sfp_bus && pl->phydev)
1872 ret = phy_speed_down(pl->phydev, sync);
1873
1874 return ret;
1875}
1876EXPORT_SYMBOL_GPL(phylink_speed_down);
1877
1878/**
1879 * phylink_speed_up() - restore the advertised speeds prior to the call to
1880 * phylink_speed_down()
1881 * @pl: a pointer to a &struct phylink returned from phylink_create()
1882 *
1883 * If we have a PHY that is not part of a SFP module, then restore the
1884 * PHY speeds as per phy_speed_up().
1885 *
1886 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
1887 */
1888int phylink_speed_up(struct phylink *pl)
1889{
1890 int ret = 0;
1891
1892 ASSERT_RTNL();
1893
1894 if (!pl->sfp_bus && pl->phydev)
1895 ret = phy_speed_up(pl->phydev);
1896
1897 return ret;
1898}
1899EXPORT_SYMBOL_GPL(phylink_speed_up);
1900
Russell King320587e62019-05-28 10:57:34 +01001901static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
1902{
1903 struct phylink *pl = upstream;
1904
1905 pl->netdev->sfp_bus = bus;
1906}
1907
1908static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
1909{
1910 struct phylink *pl = upstream;
1911
1912 pl->netdev->sfp_bus = NULL;
1913}
1914
Russell King52c95602019-12-11 10:56:45 +00001915static int phylink_sfp_config(struct phylink *pl, u8 mode,
Russell Kingc0de2f42019-12-11 10:56:40 +00001916 const unsigned long *supported,
1917 const unsigned long *advertising)
Russell Kingce0aa272017-07-25 15:03:18 +01001918{
Russell King77316762019-06-02 15:12:54 +01001919 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
Russell Kingc0de2f42019-12-11 10:56:40 +00001920 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
Russell Kingce0aa272017-07-25 15:03:18 +01001921 struct phylink_link_state config;
1922 phy_interface_t iface;
Russell Kingce0aa272017-07-25 15:03:18 +01001923 bool changed;
Russell Kingc0de2f42019-12-11 10:56:40 +00001924 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +01001925
Russell Kingc0de2f42019-12-11 10:56:40 +00001926 linkmode_copy(support, supported);
Russell Kingce0aa272017-07-25 15:03:18 +01001927
1928 memset(&config, 0, sizeof(config));
Russell Kingc0de2f42019-12-11 10:56:40 +00001929 linkmode_copy(config.advertising, advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00001930 config.interface = PHY_INTERFACE_MODE_NA;
Russell Kingce0aa272017-07-25 15:03:18 +01001931 config.speed = SPEED_UNKNOWN;
1932 config.duplex = DUPLEX_UNKNOWN;
1933 config.pause = MLO_PAUSE_AN;
1934 config.an_enabled = pl->link_config.an_enabled;
1935
1936 /* Ignore errors if we're expecting a PHY to attach later */
1937 ret = phylink_validate(pl, support, &config);
1938 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001939 phylink_err(pl, "validation with support %*pb failed: %d\n",
1940 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kinga9c79362018-02-27 15:53:02 +00001941 return ret;
1942 }
1943
Russell Kinga4516c72019-12-11 10:55:59 +00001944 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00001945 if (iface == PHY_INTERFACE_MODE_NA) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001946 phylink_err(pl,
1947 "selection of interface failed, advertisement %*pb\n",
1948 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00001949 return -EINVAL;
1950 }
1951
1952 config.interface = iface;
Russell Kingc0de2f42019-12-11 10:56:40 +00001953 linkmode_copy(support1, support);
Russell King77316762019-06-02 15:12:54 +01001954 ret = phylink_validate(pl, support1, &config);
Russell Kinga9c79362018-02-27 15:53:02 +00001955 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001956 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
Russell Kingc0de2f42019-12-11 10:56:40 +00001957 phylink_an_mode_str(mode),
Ioana Ciornei170911802019-05-28 20:38:14 +03001958 phy_modes(config.interface),
1959 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kingce0aa272017-07-25 15:03:18 +01001960 return ret;
1961 }
1962
Ioana Ciornei170911802019-05-28 20:38:14 +03001963 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
Russell Kingc0de2f42019-12-11 10:56:40 +00001964 phylink_an_mode_str(mode), phy_modes(config.interface),
Ioana Ciornei170911802019-05-28 20:38:14 +03001965 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001966
Russell King86a362c2017-12-01 10:24:26 +00001967 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001968 return -EINVAL;
1969
Russell King554032c2019-10-15 11:28:46 +01001970 changed = !linkmode_equal(pl->supported, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001971 if (changed) {
1972 linkmode_copy(pl->supported, support);
1973 linkmode_copy(pl->link_config.advertising, config.advertising);
1974 }
1975
Russell Kingc0de2f42019-12-11 10:56:40 +00001976 if (pl->cur_link_an_mode != mode ||
Russell Kingce0aa272017-07-25 15:03:18 +01001977 pl->link_config.interface != config.interface) {
1978 pl->link_config.interface = config.interface;
Russell Kingc0de2f42019-12-11 10:56:40 +00001979 pl->cur_link_an_mode = mode;
Russell Kingce0aa272017-07-25 15:03:18 +01001980
1981 changed = true;
1982
Ioana Ciornei170911802019-05-28 20:38:14 +03001983 phylink_info(pl, "switched to %s/%s link mode\n",
Russell Kingc0de2f42019-12-11 10:56:40 +00001984 phylink_an_mode_str(mode),
Ioana Ciornei170911802019-05-28 20:38:14 +03001985 phy_modes(config.interface));
Russell Kingce0aa272017-07-25 15:03:18 +01001986 }
1987
Russell King52c95602019-12-11 10:56:45 +00001988 pl->link_port = pl->sfp_port;
Russell Kingce0aa272017-07-25 15:03:18 +01001989
1990 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1991 &pl->phylink_disable_state))
Russell King4c0d6d32020-03-30 18:44:55 +01001992 phylink_mac_initial_config(pl, false);
Russell Kingce0aa272017-07-25 15:03:18 +01001993
1994 return ret;
1995}
1996
Russell Kingc0de2f42019-12-11 10:56:40 +00001997static int phylink_sfp_module_insert(void *upstream,
1998 const struct sfp_eeprom_id *id)
1999{
2000 struct phylink *pl = upstream;
Russell King52c95602019-12-11 10:56:45 +00002001 unsigned long *support = pl->sfp_support;
Russell Kingc0de2f42019-12-11 10:56:40 +00002002
2003 ASSERT_RTNL();
2004
Russell King52c95602019-12-11 10:56:45 +00002005 linkmode_zero(support);
Russell Kingc0de2f42019-12-11 10:56:40 +00002006 sfp_parse_support(pl->sfp_bus, id, support);
Russell King52c95602019-12-11 10:56:45 +00002007 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
Russell Kingc0de2f42019-12-11 10:56:40 +00002008
Russell King52c95602019-12-11 10:56:45 +00002009 /* If this module may have a PHY connecting later, defer until later */
2010 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2011 if (pl->sfp_may_have_phy)
2012 return 0;
2013
2014 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
Russell Kingc0de2f42019-12-11 10:56:40 +00002015}
2016
Russell King48820572019-12-11 10:56:14 +00002017static int phylink_sfp_module_start(void *upstream)
2018{
2019 struct phylink *pl = upstream;
2020
2021 /* If this SFP module has a PHY, start the PHY now. */
Russell King52c95602019-12-11 10:56:45 +00002022 if (pl->phydev) {
Russell King48820572019-12-11 10:56:14 +00002023 phy_start(pl->phydev);
Russell King52c95602019-12-11 10:56:45 +00002024 return 0;
2025 }
Russell King48820572019-12-11 10:56:14 +00002026
Russell King52c95602019-12-11 10:56:45 +00002027 /* If the module may have a PHY but we didn't detect one we
2028 * need to configure the MAC here.
2029 */
2030 if (!pl->sfp_may_have_phy)
2031 return 0;
2032
2033 return phylink_sfp_config(pl, MLO_AN_INBAND,
2034 pl->sfp_support, pl->sfp_support);
Russell King48820572019-12-11 10:56:14 +00002035}
2036
2037static void phylink_sfp_module_stop(void *upstream)
2038{
2039 struct phylink *pl = upstream;
2040
2041 /* If this SFP module has a PHY, stop it. */
2042 if (pl->phydev)
2043 phy_stop(pl->phydev);
2044}
2045
Russell Kingce0aa272017-07-25 15:03:18 +01002046static void phylink_sfp_link_down(void *upstream)
2047{
2048 struct phylink *pl = upstream;
2049
Russell King8b874512017-12-15 16:09:47 +00002050 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01002051
Russell King87454b62019-02-11 15:04:24 +00002052 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
Russell Kingce0aa272017-07-25 15:03:18 +01002053}
2054
2055static void phylink_sfp_link_up(void *upstream)
2056{
2057 struct phylink *pl = upstream;
2058
Russell King8b874512017-12-15 16:09:47 +00002059 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01002060
2061 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2062 phylink_run_resolve(pl);
2063}
2064
Russell King7adb5b22019-12-11 10:56:50 +00002065/* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2066 * or 802.3z control word, so inband will not work.
2067 */
2068static bool phylink_phy_no_inband(struct phy_device *phy)
2069{
2070 return phy->is_c45 &&
2071 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2072}
2073
Russell Kingce0aa272017-07-25 15:03:18 +01002074static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2075{
Baruch Siach7e418372018-10-03 19:04:49 +03002076 struct phylink *pl = upstream;
Russell King52c95602019-12-11 10:56:45 +00002077 phy_interface_t interface;
Russell King7adb5b22019-12-11 10:56:50 +00002078 u8 mode;
Russell King938d44c2019-12-11 10:56:25 +00002079 int ret;
Baruch Siach7e418372018-10-03 19:04:49 +03002080
Russell King52c95602019-12-11 10:56:45 +00002081 /*
2082 * This is the new way of dealing with flow control for PHYs,
2083 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2084 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2085 * using our validate call to the MAC, we rely upon the MAC
2086 * clearing the bits from both supported and advertising fields.
2087 */
2088 phy_support_asym_pause(phy);
2089
Russell King7adb5b22019-12-11 10:56:50 +00002090 if (phylink_phy_no_inband(phy))
2091 mode = MLO_AN_PHY;
2092 else
2093 mode = MLO_AN_INBAND;
2094
Russell King52c95602019-12-11 10:56:45 +00002095 /* Do the initial configuration */
Russell King7adb5b22019-12-11 10:56:50 +00002096 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
Russell King52c95602019-12-11 10:56:45 +00002097 if (ret < 0)
2098 return ret;
2099
2100 interface = pl->link_config.interface;
2101 ret = phylink_attach_phy(pl, phy, interface);
Russell King938d44c2019-12-11 10:56:25 +00002102 if (ret < 0)
2103 return ret;
2104
Russell Kinge45d1f52019-12-11 10:56:30 +00002105 ret = phylink_bringup_phy(pl, phy, interface);
Russell King938d44c2019-12-11 10:56:25 +00002106 if (ret)
2107 phy_detach(phy);
2108
2109 return ret;
Russell Kingce0aa272017-07-25 15:03:18 +01002110}
2111
2112static void phylink_sfp_disconnect_phy(void *upstream)
2113{
2114 phylink_disconnect_phy(upstream);
2115}
2116
2117static const struct sfp_upstream_ops sfp_phylink_ops = {
Russell King320587e62019-05-28 10:57:34 +01002118 .attach = phylink_sfp_attach,
2119 .detach = phylink_sfp_detach,
Russell Kingce0aa272017-07-25 15:03:18 +01002120 .module_insert = phylink_sfp_module_insert,
Russell King48820572019-12-11 10:56:14 +00002121 .module_start = phylink_sfp_module_start,
2122 .module_stop = phylink_sfp_module_stop,
Russell Kingce0aa272017-07-25 15:03:18 +01002123 .link_up = phylink_sfp_link_up,
2124 .link_down = phylink_sfp_link_down,
2125 .connect_phy = phylink_sfp_connect_phy,
2126 .disconnect_phy = phylink_sfp_disconnect_phy,
2127};
2128
Russell King624c0f02018-08-09 15:38:38 +02002129/* Helpers for MAC drivers */
2130
2131/**
2132 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2133 * @state: a pointer to a &struct phylink_link_state
2134 *
2135 * Inspect the interface mode, advertising mask or forced speed and
2136 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2137 * the interface mode to suit. @state->interface is appropriately
2138 * updated, and the advertising mask has the "other" baseX_Full flag
2139 * cleared.
2140 */
2141void phylink_helper_basex_speed(struct phylink_link_state *state)
2142{
2143 if (phy_interface_mode_is_8023z(state->interface)) {
2144 bool want_2500 = state->an_enabled ?
2145 phylink_test(state->advertising, 2500baseX_Full) :
2146 state->speed == SPEED_2500;
2147
2148 if (want_2500) {
2149 phylink_clear(state->advertising, 1000baseX_Full);
2150 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2151 } else {
2152 phylink_clear(state->advertising, 2500baseX_Full);
2153 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2154 }
2155 }
2156}
2157EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2158
Russell King74db1c12020-03-17 14:52:36 +00002159static void phylink_decode_c37_word(struct phylink_link_state *state,
2160 uint16_t config_reg, int speed)
2161{
2162 bool tx_pause, rx_pause;
2163 int fd_bit;
2164
2165 if (speed == SPEED_2500)
2166 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2167 else
2168 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2169
2170 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2171
2172 if (linkmode_test_bit(fd_bit, state->advertising) &&
2173 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2174 state->speed = speed;
2175 state->duplex = DUPLEX_FULL;
2176 } else {
2177 /* negotiation failure */
2178 state->link = false;
2179 }
2180
2181 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2182 &tx_pause, &rx_pause);
2183
2184 if (tx_pause)
2185 state->pause |= MLO_PAUSE_TX;
2186 if (rx_pause)
2187 state->pause |= MLO_PAUSE_RX;
2188}
2189
2190static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2191 uint16_t config_reg)
2192{
2193 if (!(config_reg & LPA_SGMII_LINK)) {
2194 state->link = false;
2195 return;
2196 }
2197
2198 switch (config_reg & LPA_SGMII_SPD_MASK) {
2199 case LPA_SGMII_10:
2200 state->speed = SPEED_10;
2201 break;
2202 case LPA_SGMII_100:
2203 state->speed = SPEED_100;
2204 break;
2205 case LPA_SGMII_1000:
2206 state->speed = SPEED_1000;
2207 break;
2208 default:
2209 state->link = false;
2210 return;
2211 }
2212 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2213 state->duplex = DUPLEX_FULL;
2214 else
2215 state->duplex = DUPLEX_HALF;
2216}
2217
2218/**
2219 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2220 * @pcs: a pointer to a &struct mdio_device.
2221 * @state: a pointer to a &struct phylink_link_state.
2222 *
2223 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2224 * clause 37 negotiation and/or SGMII control.
2225 *
2226 * Read the MAC PCS state from the MII device configured in @config and
2227 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2228 * the phylink @state structure. This is suitable to be directly plugged
2229 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2230 * structure.
2231 */
2232void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2233 struct phylink_link_state *state)
2234{
2235 struct mii_bus *bus = pcs->bus;
2236 int addr = pcs->addr;
2237 int bmsr, lpa;
2238
2239 bmsr = mdiobus_read(bus, addr, MII_BMSR);
2240 lpa = mdiobus_read(bus, addr, MII_LPA);
2241 if (bmsr < 0 || lpa < 0) {
2242 state->link = false;
2243 return;
2244 }
2245
2246 state->link = !!(bmsr & BMSR_LSTATUS);
2247 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2248 if (!state->link)
2249 return;
2250
2251 switch (state->interface) {
2252 case PHY_INTERFACE_MODE_1000BASEX:
2253 phylink_decode_c37_word(state, lpa, SPEED_1000);
2254 break;
2255
2256 case PHY_INTERFACE_MODE_2500BASEX:
2257 phylink_decode_c37_word(state, lpa, SPEED_2500);
2258 break;
2259
2260 case PHY_INTERFACE_MODE_SGMII:
2261 phylink_decode_sgmii_word(state, lpa);
2262 break;
2263
2264 default:
2265 state->link = false;
2266 break;
2267 }
2268}
2269EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2270
2271/**
2272 * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2273 * advertisement
2274 * @pcs: a pointer to a &struct mdio_device.
Russell King0bd27402020-03-30 18:44:44 +01002275 * @interface: the PHY interface mode being configured
2276 * @advertising: the ethtool advertisement mask
Russell King74db1c12020-03-17 14:52:36 +00002277 *
2278 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2279 * clause 37 negotiation and/or SGMII control.
2280 *
2281 * Configure the clause 37 PCS advertisement as specified by @state. This
2282 * does not trigger a renegotiation; phylink will do that via the
2283 * mac_an_restart() method of the struct phylink_mac_ops structure.
2284 *
2285 * Returns negative error code on failure to configure the advertisement,
2286 * zero if no change has been made, or one if the advertisement has changed.
2287 */
2288int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
Russell King0bd27402020-03-30 18:44:44 +01002289 phy_interface_t interface,
2290 const unsigned long *advertising)
Russell King74db1c12020-03-17 14:52:36 +00002291{
2292 struct mii_bus *bus = pcs->bus;
2293 int addr = pcs->addr;
2294 int val, ret;
2295 u16 adv;
2296
Russell King0bd27402020-03-30 18:44:44 +01002297 switch (interface) {
Russell King74db1c12020-03-17 14:52:36 +00002298 case PHY_INTERFACE_MODE_1000BASEX:
2299 case PHY_INTERFACE_MODE_2500BASEX:
2300 adv = ADVERTISE_1000XFULL;
2301 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
Russell King0bd27402020-03-30 18:44:44 +01002302 advertising))
Russell King74db1c12020-03-17 14:52:36 +00002303 adv |= ADVERTISE_1000XPAUSE;
2304 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
Russell King0bd27402020-03-30 18:44:44 +01002305 advertising))
Russell King74db1c12020-03-17 14:52:36 +00002306 adv |= ADVERTISE_1000XPSE_ASYM;
2307
2308 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2309 if (val < 0)
2310 return val;
2311
2312 if (val == adv)
2313 return 0;
2314
2315 ret = mdiobus_write(bus, addr, MII_ADVERTISE, adv);
2316 if (ret < 0)
2317 return ret;
2318
2319 return 1;
2320
2321 case PHY_INTERFACE_MODE_SGMII:
2322 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2323 if (val < 0)
2324 return val;
2325
2326 if (val == 0x0001)
2327 return 0;
2328
2329 ret = mdiobus_write(bus, addr, MII_ADVERTISE, 0x0001);
2330 if (ret < 0)
2331 return ret;
2332
2333 return 1;
2334
2335 default:
2336 /* Nothing to do for other modes */
2337 return 0;
2338 }
2339}
2340EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2341
2342/**
2343 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2344 * @pcs: a pointer to a &struct mdio_device.
2345 *
2346 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2347 * clause 37 negotiation.
2348 *
2349 * Restart the clause 37 negotiation with the link partner. This is
2350 * suitable to be directly plugged into the mac_pcs_get_state() member
2351 * of the struct phylink_mac_ops structure.
2352 */
2353void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2354{
2355 struct mii_bus *bus = pcs->bus;
2356 int val, addr = pcs->addr;
2357
2358 val = mdiobus_read(bus, addr, MII_BMCR);
2359 if (val >= 0) {
2360 val |= BMCR_ANRESTART;
2361
2362 mdiobus_write(bus, addr, MII_BMCR, val);
2363 }
2364}
2365EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2366
Russell Kingb8679ef2020-03-17 14:52:41 +00002367void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2368 struct phylink_link_state *state)
2369{
2370 struct mii_bus *bus = pcs->bus;
2371 int addr = pcs->addr;
2372 int stat;
2373
Russell King90ce6652020-05-26 16:29:36 +01002374 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
Russell Kingb8679ef2020-03-17 14:52:41 +00002375 if (stat < 0) {
2376 state->link = false;
2377 return;
2378 }
2379
2380 state->link = !!(stat & MDIO_STAT1_LSTATUS);
2381 if (!state->link)
2382 return;
2383
2384 switch (state->interface) {
2385 case PHY_INTERFACE_MODE_10GBASER:
2386 state->speed = SPEED_10000;
2387 state->duplex = DUPLEX_FULL;
2388 break;
2389
2390 default:
2391 break;
2392 }
2393}
2394EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2395
Andrew Lunn5f857572019-01-21 19:10:19 +01002396MODULE_LICENSE("GPL v2");