blob: 452d509803ca166afe4928595ae2d407ac77e028 [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
Russell King1e1bf142020-07-21 12:04:31 +01001363 /* If we have a fixed link, refuse to change link parameters.
1364 * If the link parameters match, accept them but do nothing.
Russell King9525ae82017-07-25 15:03:13 +01001365 */
Russell King1e1bf142020-07-21 12:04:31 +01001366 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1367 if (s->speed != pl->link_config.speed ||
1368 s->duplex != pl->link_config.duplex)
1369 return -EINVAL;
1370 return 0;
1371 }
Russell King9525ae82017-07-25 15:03:13 +01001372
1373 config.speed = s->speed;
1374 config.duplex = s->duplex;
Russell Kingc8cab712020-07-21 12:04:16 +01001375 break;
Russell King9525ae82017-07-25 15:03:13 +01001376
Russell Kingc8cab712020-07-21 12:04:16 +01001377 case AUTONEG_ENABLE:
Russell King1e1bf142020-07-21 12:04:31 +01001378 /* If we have a fixed link, allow autonegotiation (since that
1379 * is our default case) but do not allow the advertisement to
1380 * be changed. If the advertisement matches, simply return.
1381 */
1382 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1383 if (!linkmode_equal(config.advertising,
1384 pl->link_config.advertising))
1385 return -EINVAL;
1386 return 0;
1387 }
Russell King9525ae82017-07-25 15:03:13 +01001388
1389 config.speed = SPEED_UNKNOWN;
1390 config.duplex = DUPLEX_UNKNOWN;
Russell Kingc8cab712020-07-21 12:04:16 +01001391 break;
Russell King9525ae82017-07-25 15:03:13 +01001392
Russell Kingc8cab712020-07-21 12:04:16 +01001393 default:
1394 return -EINVAL;
Russell King9525ae82017-07-25 15:03:13 +01001395 }
1396
Russell King1e1bf142020-07-21 12:04:31 +01001397 /* We have ruled out the case with a PHY attached, and the
1398 * fixed-link cases. All that is left are in-band links.
Russell Kingcbc1bb12020-07-21 12:04:21 +01001399 */
1400 if (phylink_validate(pl, support, &config))
1401 return -EINVAL;
1402
1403 /* If autonegotiation is enabled, we must have an advertisement */
1404 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1405 return -EINVAL;
1406
1407 mutex_lock(&pl->state_mutex);
1408 linkmode_copy(pl->link_config.advertising, config.advertising);
1409 pl->link_config.interface = config.interface;
1410 pl->link_config.speed = config.speed;
1411 pl->link_config.duplex = config.duplex;
Russell Kinga83c8822020-07-21 12:04:26 +01001412 pl->link_config.an_enabled = config.an_enabled;
Russell Kingcbc1bb12020-07-21 12:04:21 +01001413
1414 if (pl->cur_link_an_mode == MLO_AN_INBAND &&
1415 !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1416 /* If in 802.3z mode, this updates the advertisement.
1417 *
1418 * If we are in SGMII mode without a PHY, there is no
1419 * advertisement; the only thing we have is the pause
1420 * modes which can only come from a PHY.
Russell King5d57c322019-12-13 18:22:02 +00001421 */
Russell Kingcbc1bb12020-07-21 12:04:21 +01001422 phylink_pcs_config(pl, true, &pl->link_config);
Russell King9525ae82017-07-25 15:03:13 +01001423 }
Russell Kingcbc1bb12020-07-21 12:04:21 +01001424 mutex_unlock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +01001425
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001426 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001427}
1428EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1429
Russell King8796c892017-12-01 10:24:47 +00001430/**
1431 * phylink_ethtool_nway_reset() - restart negotiation
1432 * @pl: a pointer to a &struct phylink returned from phylink_create()
1433 *
1434 * Restart negotiation for the phylink instance specified by @pl. This will
1435 * cause any attached phy to restart negotiation with the link partner, and
1436 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1437 * negotiation.
1438 *
1439 * Returns zero on success, or negative error code.
1440 */
Russell King9525ae82017-07-25 15:03:13 +01001441int phylink_ethtool_nway_reset(struct phylink *pl)
1442{
1443 int ret = 0;
1444
Russell King8b874512017-12-15 16:09:47 +00001445 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001446
1447 if (pl->phydev)
1448 ret = phy_restart_aneg(pl->phydev);
Russell King4c0d6d32020-03-30 18:44:55 +01001449 phylink_mac_pcs_an_restart(pl);
Russell King9525ae82017-07-25 15:03:13 +01001450
1451 return ret;
1452}
1453EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1454
Russell King8796c892017-12-01 10:24:47 +00001455/**
1456 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1457 * @pl: a pointer to a &struct phylink returned from phylink_create()
1458 * @pause: a pointer to a &struct ethtool_pauseparam
1459 */
Russell King9525ae82017-07-25 15:03:13 +01001460void phylink_ethtool_get_pauseparam(struct phylink *pl,
1461 struct ethtool_pauseparam *pause)
1462{
Russell King8b874512017-12-15 16:09:47 +00001463 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001464
1465 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1466 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1467 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1468}
1469EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1470
Russell King8796c892017-12-01 10:24:47 +00001471/**
1472 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1473 * @pl: a pointer to a &struct phylink returned from phylink_create()
1474 * @pause: a pointer to a &struct ethtool_pauseparam
1475 */
Russell King9525ae82017-07-25 15:03:13 +01001476int phylink_ethtool_set_pauseparam(struct phylink *pl,
1477 struct ethtool_pauseparam *pause)
1478{
1479 struct phylink_link_state *config = &pl->link_config;
Russell King2e919bc2020-06-23 17:47:29 +01001480 bool manual_changed;
1481 int pause_state;
Russell King9525ae82017-07-25 15:03:13 +01001482
Russell King8b874512017-12-15 16:09:47 +00001483 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001484
Russell King8cdfa252020-02-15 15:49:37 +00001485 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1486 return -EOPNOTSUPP;
1487
Russell King9525ae82017-07-25 15:03:13 +01001488 if (!phylink_test(pl->supported, Pause) &&
1489 !phylink_test(pl->supported, Asym_Pause))
1490 return -EOPNOTSUPP;
1491
1492 if (!phylink_test(pl->supported, Asym_Pause) &&
1493 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1494 return -EINVAL;
1495
Russell King2e919bc2020-06-23 17:47:29 +01001496 pause_state = 0;
Russell King9525ae82017-07-25 15:03:13 +01001497 if (pause->autoneg)
Russell King2e919bc2020-06-23 17:47:29 +01001498 pause_state |= MLO_PAUSE_AN;
Russell King9525ae82017-07-25 15:03:13 +01001499 if (pause->rx_pause)
Russell King2e919bc2020-06-23 17:47:29 +01001500 pause_state |= MLO_PAUSE_RX;
Russell King9525ae82017-07-25 15:03:13 +01001501 if (pause->tx_pause)
Russell King2e919bc2020-06-23 17:47:29 +01001502 pause_state |= MLO_PAUSE_TX;
Russell King9525ae82017-07-25 15:03:13 +01001503
Russell King2e919bc2020-06-23 17:47:29 +01001504 mutex_lock(&pl->state_mutex);
Russell Kingf904f152020-02-15 15:49:58 +00001505 /*
1506 * See the comments for linkmode_set_pause(), wrt the deficiencies
1507 * with the current implementation. A solution to this issue would
1508 * be:
1509 * ethtool Local device
1510 * rx tx Pause AsymDir
1511 * 0 0 0 0
1512 * 1 0 1 1
1513 * 0 1 0 1
1514 * 1 1 1 1
1515 * and then use the ethtool rx/tx enablement status to mask the
1516 * rx/tx pause resolution.
1517 */
1518 linkmode_set_pause(config->advertising, pause->tx_pause,
1519 pause->rx_pause);
1520
Russell King2e919bc2020-06-23 17:47:29 +01001521 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1522 (!(pause_state & MLO_PAUSE_AN) &&
1523 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1524
1525 config->pause = pause_state;
1526
Russell Kingc718af22020-06-23 17:47:23 +01001527 if (!pl->phydev && !test_bit(PHYLINK_DISABLE_STOPPED,
1528 &pl->phylink_disable_state))
1529 phylink_pcs_config(pl, true, &pl->link_config);
1530
1531 mutex_unlock(&pl->state_mutex);
1532
1533 /* If we have a PHY, a change of the pause frame advertisement will
1534 * cause phylib to renegotiate (if AN is enabled) which will in turn
1535 * call our phylink_phy_change() and trigger a resolve. Note that
1536 * we can't hold our state mutex while calling phy_set_asym_pause().
Russell Kingd9922c02019-11-19 17:28:14 +00001537 */
Russell Kingc718af22020-06-23 17:47:23 +01001538 if (pl->phydev)
Russell Kingd9922c02019-11-19 17:28:14 +00001539 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1540 pause->tx_pause);
Russell King9525ae82017-07-25 15:03:13 +01001541
Russell King2e919bc2020-06-23 17:47:29 +01001542 /* If the manual pause settings changed, make sure we trigger a
1543 * resolve to update their state; we can not guarantee that the
1544 * link will cycle.
1545 */
1546 if (manual_changed) {
1547 pl->mac_link_dropped = true;
1548 phylink_run_resolve(pl);
Russell King9525ae82017-07-25 15:03:13 +01001549 }
Russell King9525ae82017-07-25 15:03:13 +01001550
1551 return 0;
1552}
1553EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1554
1555/**
1556 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1557 * counter
1558 * @pl: a pointer to a &struct phylink returned from phylink_create().
1559 *
1560 * Read the Energy Efficient Ethernet error counter from the PHY associated
1561 * with the phylink instance specified by @pl.
1562 *
1563 * Returns positive error counter value, or negative error code.
1564 */
1565int phylink_get_eee_err(struct phylink *pl)
1566{
1567 int ret = 0;
1568
1569 ASSERT_RTNL();
1570
1571 if (pl->phydev)
1572 ret = phy_get_eee_err(pl->phydev);
1573
1574 return ret;
1575}
1576EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1577
1578/**
Russell King86e58132019-02-11 11:46:06 +00001579 * phylink_init_eee() - init and check the EEE features
1580 * @pl: a pointer to a &struct phylink returned from phylink_create()
1581 * @clk_stop_enable: allow PHY to stop receive clock
1582 *
1583 * Must be called either with RTNL held or within mac_link_up()
1584 */
1585int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1586{
1587 int ret = -EOPNOTSUPP;
1588
1589 if (pl->phydev)
1590 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1591
1592 return ret;
1593}
1594EXPORT_SYMBOL_GPL(phylink_init_eee);
1595
1596/**
Russell King8796c892017-12-01 10:24:47 +00001597 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1598 * @pl: a pointer to a &struct phylink returned from phylink_create()
1599 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1600 */
Russell King9525ae82017-07-25 15:03:13 +01001601int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1602{
1603 int ret = -EOPNOTSUPP;
1604
Russell King8b874512017-12-15 16:09:47 +00001605 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001606
1607 if (pl->phydev)
1608 ret = phy_ethtool_get_eee(pl->phydev, eee);
1609
1610 return ret;
1611}
1612EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1613
Russell King8796c892017-12-01 10:24:47 +00001614/**
1615 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1616 * @pl: a pointer to a &struct phylink returned from phylink_create()
1617 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1618 */
Russell King9525ae82017-07-25 15:03:13 +01001619int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1620{
1621 int ret = -EOPNOTSUPP;
1622
Russell King8b874512017-12-15 16:09:47 +00001623 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001624
1625 if (pl->phydev)
1626 ret = phy_ethtool_set_eee(pl->phydev, eee);
1627
1628 return ret;
1629}
1630EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1631
1632/* This emulates MII registers for a fixed-mode phy operating as per the
1633 * passed in state. "aneg" defines if we report negotiation is possible.
1634 *
1635 * FIXME: should deal with negotiation state too.
1636 */
Russell King7fdc4552019-05-28 10:57:18 +01001637static int phylink_mii_emul_read(unsigned int reg,
1638 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +01001639{
1640 struct fixed_phy_status fs;
Russell King4e5aeb42020-02-15 15:49:53 +00001641 unsigned long *lpa = state->lp_advertising;
Russell King9525ae82017-07-25 15:03:13 +01001642 int val;
1643
1644 fs.link = state->link;
1645 fs.speed = state->speed;
1646 fs.duplex = state->duplex;
Russell King4e5aeb42020-02-15 15:49:53 +00001647 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1648 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
Russell King9525ae82017-07-25 15:03:13 +01001649
1650 val = swphy_read_reg(reg, &fs);
1651 if (reg == MII_BMSR) {
1652 if (!state->an_complete)
1653 val &= ~BMSR_ANEGCOMPLETE;
Russell King9525ae82017-07-25 15:03:13 +01001654 }
1655 return val;
1656}
1657
Russell Kingecbd87b2017-07-25 15:03:28 +01001658static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1659 unsigned int reg)
1660{
1661 struct phy_device *phydev = pl->phydev;
1662 int prtad, devad;
1663
1664 if (mdio_phy_id_is_c45(phy_id)) {
1665 prtad = mdio_phy_id_prtad(phy_id);
1666 devad = mdio_phy_id_devad(phy_id);
Russell King90ce6652020-05-26 16:29:36 +01001667 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001668 } else if (phydev->is_c45) {
1669 switch (reg) {
1670 case MII_BMCR:
1671 case MII_BMSR:
1672 case MII_PHYSID1:
1673 case MII_PHYSID2:
Russell King320ed3b2020-06-18 14:46:08 +01001674 devad = __ffs(phydev->c45_ids.mmds_present);
Russell Kingecbd87b2017-07-25 15:03:28 +01001675 break;
1676 case MII_ADVERTISE:
1677 case MII_LPA:
Russell King320ed3b2020-06-18 14:46:08 +01001678 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
Russell Kingecbd87b2017-07-25 15:03:28 +01001679 return -EINVAL;
1680 devad = MDIO_MMD_AN;
1681 if (reg == MII_ADVERTISE)
1682 reg = MDIO_AN_ADVERTISE;
1683 else
1684 reg = MDIO_AN_LPA;
1685 break;
1686 default:
1687 return -EINVAL;
1688 }
1689 prtad = phy_id;
Russell King90ce6652020-05-26 16:29:36 +01001690 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001691 } else {
1692 prtad = phy_id;
1693 devad = reg;
1694 }
1695 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1696}
1697
1698static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1699 unsigned int reg, unsigned int val)
1700{
1701 struct phy_device *phydev = pl->phydev;
1702 int prtad, devad;
1703
1704 if (mdio_phy_id_is_c45(phy_id)) {
1705 prtad = mdio_phy_id_prtad(phy_id);
1706 devad = mdio_phy_id_devad(phy_id);
Russell King90ce6652020-05-26 16:29:36 +01001707 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001708 } else if (phydev->is_c45) {
1709 switch (reg) {
1710 case MII_BMCR:
1711 case MII_BMSR:
1712 case MII_PHYSID1:
1713 case MII_PHYSID2:
Russell King320ed3b2020-06-18 14:46:08 +01001714 devad = __ffs(phydev->c45_ids.mmds_present);
Russell Kingecbd87b2017-07-25 15:03:28 +01001715 break;
1716 case MII_ADVERTISE:
1717 case MII_LPA:
Russell King320ed3b2020-06-18 14:46:08 +01001718 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
Russell Kingecbd87b2017-07-25 15:03:28 +01001719 return -EINVAL;
1720 devad = MDIO_MMD_AN;
1721 if (reg == MII_ADVERTISE)
1722 reg = MDIO_AN_ADVERTISE;
1723 else
1724 reg = MDIO_AN_LPA;
1725 break;
1726 default:
1727 return -EINVAL;
1728 }
1729 prtad = phy_id;
Russell King90ce6652020-05-26 16:29:36 +01001730 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001731 } else {
1732 prtad = phy_id;
1733 devad = reg;
1734 }
1735
1736 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1737}
1738
Russell King9525ae82017-07-25 15:03:13 +01001739static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1740 unsigned int reg)
1741{
1742 struct phylink_link_state state;
1743 int val = 0xffff;
1744
Russell King24cf0e62019-12-11 10:56:35 +00001745 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +01001746 case MLO_AN_FIXED:
1747 if (phy_id == 0) {
1748 phylink_get_fixed_state(pl, &state);
Russell King7fdc4552019-05-28 10:57:18 +01001749 val = phylink_mii_emul_read(reg, &state);
Russell King9525ae82017-07-25 15:03:13 +01001750 }
1751 break;
1752
1753 case MLO_AN_PHY:
1754 return -EOPNOTSUPP;
1755
Russell King86a362c2017-12-01 10:24:26 +00001756 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001757 if (phy_id == 0) {
Russell Kingd46b7e42019-11-21 00:36:22 +00001758 phylink_mac_pcs_get_state(pl, &state);
Russell King7fdc4552019-05-28 10:57:18 +01001759 val = phylink_mii_emul_read(reg, &state);
Russell King9525ae82017-07-25 15:03:13 +01001760 }
1761 break;
1762 }
1763
1764 return val & 0xffff;
1765}
1766
1767static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1768 unsigned int reg, unsigned int val)
1769{
Russell King24cf0e62019-12-11 10:56:35 +00001770 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +01001771 case MLO_AN_FIXED:
1772 break;
1773
1774 case MLO_AN_PHY:
1775 return -EOPNOTSUPP;
1776
Russell King86a362c2017-12-01 10:24:26 +00001777 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001778 break;
1779 }
1780
1781 return 0;
1782}
1783
Russell King8796c892017-12-01 10:24:47 +00001784/**
1785 * phylink_mii_ioctl() - generic mii ioctl interface
1786 * @pl: a pointer to a &struct phylink returned from phylink_create()
1787 * @ifr: a pointer to a &struct ifreq for socket ioctls
1788 * @cmd: ioctl cmd to execute
1789 *
1790 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1791 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1792 *
1793 * Returns: zero on success or negative error code.
1794 *
1795 * %SIOCGMIIPHY:
1796 * read register from the current PHY.
1797 * %SIOCGMIIREG:
1798 * read register from the specified PHY.
1799 * %SIOCSMIIREG:
1800 * set a register on the specified PHY.
1801 */
Russell King9525ae82017-07-25 15:03:13 +01001802int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1803{
Russell Kingecbd87b2017-07-25 15:03:28 +01001804 struct mii_ioctl_data *mii = if_mii(ifr);
1805 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001806
Russell King8b874512017-12-15 16:09:47 +00001807 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001808
Russell Kingecbd87b2017-07-25 15:03:28 +01001809 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001810 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001811 switch (cmd) {
1812 case SIOCGMIIPHY:
1813 mii->phy_id = pl->phydev->mdio.addr;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001814 /* fall through */
Russell King9525ae82017-07-25 15:03:13 +01001815
Russell Kingecbd87b2017-07-25 15:03:28 +01001816 case SIOCGMIIREG:
1817 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1818 if (ret >= 0) {
1819 mii->val_out = ret;
1820 ret = 0;
1821 }
1822 break;
Russell King9525ae82017-07-25 15:03:13 +01001823
Russell Kingecbd87b2017-07-25 15:03:28 +01001824 case SIOCSMIIREG:
1825 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1826 mii->val_in);
1827 break;
Russell King9525ae82017-07-25 15:03:13 +01001828
Russell Kingecbd87b2017-07-25 15:03:28 +01001829 default:
Russell King9525ae82017-07-25 15:03:13 +01001830 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001831 break;
1832 }
1833 } else {
1834 switch (cmd) {
1835 case SIOCGMIIPHY:
1836 mii->phy_id = 0;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001837 /* fall through */
Russell Kingecbd87b2017-07-25 15:03:28 +01001838
1839 case SIOCGMIIREG:
1840 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1841 if (ret >= 0) {
1842 mii->val_out = ret;
1843 ret = 0;
1844 }
1845 break;
1846
1847 case SIOCSMIIREG:
1848 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1849 mii->val_in);
1850 break;
1851
1852 default:
1853 ret = -EOPNOTSUPP;
1854 break;
1855 }
Russell King9525ae82017-07-25 15:03:13 +01001856 }
1857
1858 return ret;
1859}
1860EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1861
Russell Kingc6d5d842020-06-24 11:06:54 +01001862/**
1863 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
1864 * link partners
1865 * @pl: a pointer to a &struct phylink returned from phylink_create()
1866 * @sync: perform action synchronously
1867 *
1868 * If we have a PHY that is not part of a SFP module, then set the speed
1869 * as described in the phy_speed_down() function. Please see this function
1870 * for a description of the @sync parameter.
1871 *
1872 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
1873 */
1874int phylink_speed_down(struct phylink *pl, bool sync)
1875{
1876 int ret = 0;
1877
1878 ASSERT_RTNL();
1879
1880 if (!pl->sfp_bus && pl->phydev)
1881 ret = phy_speed_down(pl->phydev, sync);
1882
1883 return ret;
1884}
1885EXPORT_SYMBOL_GPL(phylink_speed_down);
1886
1887/**
1888 * phylink_speed_up() - restore the advertised speeds prior to the call to
1889 * phylink_speed_down()
1890 * @pl: a pointer to a &struct phylink returned from phylink_create()
1891 *
1892 * If we have a PHY that is not part of a SFP module, then restore the
1893 * PHY speeds as per phy_speed_up().
1894 *
1895 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
1896 */
1897int phylink_speed_up(struct phylink *pl)
1898{
1899 int ret = 0;
1900
1901 ASSERT_RTNL();
1902
1903 if (!pl->sfp_bus && pl->phydev)
1904 ret = phy_speed_up(pl->phydev);
1905
1906 return ret;
1907}
1908EXPORT_SYMBOL_GPL(phylink_speed_up);
1909
Russell King320587e62019-05-28 10:57:34 +01001910static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
1911{
1912 struct phylink *pl = upstream;
1913
1914 pl->netdev->sfp_bus = bus;
1915}
1916
1917static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
1918{
1919 struct phylink *pl = upstream;
1920
1921 pl->netdev->sfp_bus = NULL;
1922}
1923
Russell King52c95602019-12-11 10:56:45 +00001924static int phylink_sfp_config(struct phylink *pl, u8 mode,
Russell Kingc0de2f42019-12-11 10:56:40 +00001925 const unsigned long *supported,
1926 const unsigned long *advertising)
Russell Kingce0aa272017-07-25 15:03:18 +01001927{
Russell King77316762019-06-02 15:12:54 +01001928 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
Russell Kingc0de2f42019-12-11 10:56:40 +00001929 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
Russell Kingce0aa272017-07-25 15:03:18 +01001930 struct phylink_link_state config;
1931 phy_interface_t iface;
Russell Kingce0aa272017-07-25 15:03:18 +01001932 bool changed;
Russell Kingc0de2f42019-12-11 10:56:40 +00001933 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +01001934
Russell Kingc0de2f42019-12-11 10:56:40 +00001935 linkmode_copy(support, supported);
Russell Kingce0aa272017-07-25 15:03:18 +01001936
1937 memset(&config, 0, sizeof(config));
Russell Kingc0de2f42019-12-11 10:56:40 +00001938 linkmode_copy(config.advertising, advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00001939 config.interface = PHY_INTERFACE_MODE_NA;
Russell Kingce0aa272017-07-25 15:03:18 +01001940 config.speed = SPEED_UNKNOWN;
1941 config.duplex = DUPLEX_UNKNOWN;
1942 config.pause = MLO_PAUSE_AN;
1943 config.an_enabled = pl->link_config.an_enabled;
1944
1945 /* Ignore errors if we're expecting a PHY to attach later */
1946 ret = phylink_validate(pl, support, &config);
1947 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001948 phylink_err(pl, "validation with support %*pb failed: %d\n",
1949 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kinga9c79362018-02-27 15:53:02 +00001950 return ret;
1951 }
1952
Russell Kinga4516c72019-12-11 10:55:59 +00001953 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00001954 if (iface == PHY_INTERFACE_MODE_NA) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001955 phylink_err(pl,
1956 "selection of interface failed, advertisement %*pb\n",
1957 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00001958 return -EINVAL;
1959 }
1960
1961 config.interface = iface;
Russell Kingc0de2f42019-12-11 10:56:40 +00001962 linkmode_copy(support1, support);
Russell King77316762019-06-02 15:12:54 +01001963 ret = phylink_validate(pl, support1, &config);
Russell Kinga9c79362018-02-27 15:53:02 +00001964 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001965 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
Russell Kingc0de2f42019-12-11 10:56:40 +00001966 phylink_an_mode_str(mode),
Ioana Ciornei170911802019-05-28 20:38:14 +03001967 phy_modes(config.interface),
1968 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kingce0aa272017-07-25 15:03:18 +01001969 return ret;
1970 }
1971
Ioana Ciornei170911802019-05-28 20:38:14 +03001972 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
Russell Kingc0de2f42019-12-11 10:56:40 +00001973 phylink_an_mode_str(mode), phy_modes(config.interface),
Ioana Ciornei170911802019-05-28 20:38:14 +03001974 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001975
Russell King86a362c2017-12-01 10:24:26 +00001976 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001977 return -EINVAL;
1978
Russell King554032c2019-10-15 11:28:46 +01001979 changed = !linkmode_equal(pl->supported, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001980 if (changed) {
1981 linkmode_copy(pl->supported, support);
1982 linkmode_copy(pl->link_config.advertising, config.advertising);
1983 }
1984
Russell Kingc0de2f42019-12-11 10:56:40 +00001985 if (pl->cur_link_an_mode != mode ||
Russell Kingce0aa272017-07-25 15:03:18 +01001986 pl->link_config.interface != config.interface) {
1987 pl->link_config.interface = config.interface;
Russell Kingc0de2f42019-12-11 10:56:40 +00001988 pl->cur_link_an_mode = mode;
Russell Kingce0aa272017-07-25 15:03:18 +01001989
1990 changed = true;
1991
Ioana Ciornei170911802019-05-28 20:38:14 +03001992 phylink_info(pl, "switched to %s/%s link mode\n",
Russell Kingc0de2f42019-12-11 10:56:40 +00001993 phylink_an_mode_str(mode),
Ioana Ciornei170911802019-05-28 20:38:14 +03001994 phy_modes(config.interface));
Russell Kingce0aa272017-07-25 15:03:18 +01001995 }
1996
Russell King52c95602019-12-11 10:56:45 +00001997 pl->link_port = pl->sfp_port;
Russell Kingce0aa272017-07-25 15:03:18 +01001998
1999 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2000 &pl->phylink_disable_state))
Russell King4c0d6d32020-03-30 18:44:55 +01002001 phylink_mac_initial_config(pl, false);
Russell Kingce0aa272017-07-25 15:03:18 +01002002
2003 return ret;
2004}
2005
Russell Kingc0de2f42019-12-11 10:56:40 +00002006static int phylink_sfp_module_insert(void *upstream,
2007 const struct sfp_eeprom_id *id)
2008{
2009 struct phylink *pl = upstream;
Russell King52c95602019-12-11 10:56:45 +00002010 unsigned long *support = pl->sfp_support;
Russell Kingc0de2f42019-12-11 10:56:40 +00002011
2012 ASSERT_RTNL();
2013
Russell King52c95602019-12-11 10:56:45 +00002014 linkmode_zero(support);
Russell Kingc0de2f42019-12-11 10:56:40 +00002015 sfp_parse_support(pl->sfp_bus, id, support);
Russell King52c95602019-12-11 10:56:45 +00002016 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
Russell Kingc0de2f42019-12-11 10:56:40 +00002017
Russell King52c95602019-12-11 10:56:45 +00002018 /* If this module may have a PHY connecting later, defer until later */
2019 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2020 if (pl->sfp_may_have_phy)
2021 return 0;
2022
2023 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
Russell Kingc0de2f42019-12-11 10:56:40 +00002024}
2025
Russell King48820572019-12-11 10:56:14 +00002026static int phylink_sfp_module_start(void *upstream)
2027{
2028 struct phylink *pl = upstream;
2029
2030 /* If this SFP module has a PHY, start the PHY now. */
Russell King52c95602019-12-11 10:56:45 +00002031 if (pl->phydev) {
Russell King48820572019-12-11 10:56:14 +00002032 phy_start(pl->phydev);
Russell King52c95602019-12-11 10:56:45 +00002033 return 0;
2034 }
Russell King48820572019-12-11 10:56:14 +00002035
Russell King52c95602019-12-11 10:56:45 +00002036 /* If the module may have a PHY but we didn't detect one we
2037 * need to configure the MAC here.
2038 */
2039 if (!pl->sfp_may_have_phy)
2040 return 0;
2041
2042 return phylink_sfp_config(pl, MLO_AN_INBAND,
2043 pl->sfp_support, pl->sfp_support);
Russell King48820572019-12-11 10:56:14 +00002044}
2045
2046static void phylink_sfp_module_stop(void *upstream)
2047{
2048 struct phylink *pl = upstream;
2049
2050 /* If this SFP module has a PHY, stop it. */
2051 if (pl->phydev)
2052 phy_stop(pl->phydev);
2053}
2054
Russell Kingce0aa272017-07-25 15:03:18 +01002055static void phylink_sfp_link_down(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
Russell King87454b62019-02-11 15:04:24 +00002061 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
Russell Kingce0aa272017-07-25 15:03:18 +01002062}
2063
2064static void phylink_sfp_link_up(void *upstream)
2065{
2066 struct phylink *pl = upstream;
2067
Russell King8b874512017-12-15 16:09:47 +00002068 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01002069
2070 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2071 phylink_run_resolve(pl);
2072}
2073
Russell King7adb5b22019-12-11 10:56:50 +00002074/* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2075 * or 802.3z control word, so inband will not work.
2076 */
2077static bool phylink_phy_no_inband(struct phy_device *phy)
2078{
2079 return phy->is_c45 &&
2080 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2081}
2082
Russell Kingce0aa272017-07-25 15:03:18 +01002083static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2084{
Baruch Siach7e418372018-10-03 19:04:49 +03002085 struct phylink *pl = upstream;
Russell King52c95602019-12-11 10:56:45 +00002086 phy_interface_t interface;
Russell King7adb5b22019-12-11 10:56:50 +00002087 u8 mode;
Russell King938d44c2019-12-11 10:56:25 +00002088 int ret;
Baruch Siach7e418372018-10-03 19:04:49 +03002089
Russell King52c95602019-12-11 10:56:45 +00002090 /*
2091 * This is the new way of dealing with flow control for PHYs,
2092 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2093 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2094 * using our validate call to the MAC, we rely upon the MAC
2095 * clearing the bits from both supported and advertising fields.
2096 */
2097 phy_support_asym_pause(phy);
2098
Russell King7adb5b22019-12-11 10:56:50 +00002099 if (phylink_phy_no_inband(phy))
2100 mode = MLO_AN_PHY;
2101 else
2102 mode = MLO_AN_INBAND;
2103
Russell King52c95602019-12-11 10:56:45 +00002104 /* Do the initial configuration */
Russell King7adb5b22019-12-11 10:56:50 +00002105 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
Russell King52c95602019-12-11 10:56:45 +00002106 if (ret < 0)
2107 return ret;
2108
2109 interface = pl->link_config.interface;
2110 ret = phylink_attach_phy(pl, phy, interface);
Russell King938d44c2019-12-11 10:56:25 +00002111 if (ret < 0)
2112 return ret;
2113
Russell Kinge45d1f52019-12-11 10:56:30 +00002114 ret = phylink_bringup_phy(pl, phy, interface);
Russell King938d44c2019-12-11 10:56:25 +00002115 if (ret)
2116 phy_detach(phy);
2117
2118 return ret;
Russell Kingce0aa272017-07-25 15:03:18 +01002119}
2120
2121static void phylink_sfp_disconnect_phy(void *upstream)
2122{
2123 phylink_disconnect_phy(upstream);
2124}
2125
2126static const struct sfp_upstream_ops sfp_phylink_ops = {
Russell King320587e62019-05-28 10:57:34 +01002127 .attach = phylink_sfp_attach,
2128 .detach = phylink_sfp_detach,
Russell Kingce0aa272017-07-25 15:03:18 +01002129 .module_insert = phylink_sfp_module_insert,
Russell King48820572019-12-11 10:56:14 +00002130 .module_start = phylink_sfp_module_start,
2131 .module_stop = phylink_sfp_module_stop,
Russell Kingce0aa272017-07-25 15:03:18 +01002132 .link_up = phylink_sfp_link_up,
2133 .link_down = phylink_sfp_link_down,
2134 .connect_phy = phylink_sfp_connect_phy,
2135 .disconnect_phy = phylink_sfp_disconnect_phy,
2136};
2137
Russell King624c0f02018-08-09 15:38:38 +02002138/* Helpers for MAC drivers */
2139
2140/**
2141 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2142 * @state: a pointer to a &struct phylink_link_state
2143 *
2144 * Inspect the interface mode, advertising mask or forced speed and
2145 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2146 * the interface mode to suit. @state->interface is appropriately
2147 * updated, and the advertising mask has the "other" baseX_Full flag
2148 * cleared.
2149 */
2150void phylink_helper_basex_speed(struct phylink_link_state *state)
2151{
2152 if (phy_interface_mode_is_8023z(state->interface)) {
2153 bool want_2500 = state->an_enabled ?
2154 phylink_test(state->advertising, 2500baseX_Full) :
2155 state->speed == SPEED_2500;
2156
2157 if (want_2500) {
2158 phylink_clear(state->advertising, 1000baseX_Full);
2159 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2160 } else {
2161 phylink_clear(state->advertising, 2500baseX_Full);
2162 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2163 }
2164 }
2165}
2166EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2167
Russell King74db1c12020-03-17 14:52:36 +00002168static void phylink_decode_c37_word(struct phylink_link_state *state,
2169 uint16_t config_reg, int speed)
2170{
2171 bool tx_pause, rx_pause;
2172 int fd_bit;
2173
2174 if (speed == SPEED_2500)
2175 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2176 else
2177 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2178
2179 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2180
2181 if (linkmode_test_bit(fd_bit, state->advertising) &&
2182 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2183 state->speed = speed;
2184 state->duplex = DUPLEX_FULL;
2185 } else {
2186 /* negotiation failure */
2187 state->link = false;
2188 }
2189
2190 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2191 &tx_pause, &rx_pause);
2192
2193 if (tx_pause)
2194 state->pause |= MLO_PAUSE_TX;
2195 if (rx_pause)
2196 state->pause |= MLO_PAUSE_RX;
2197}
2198
2199static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2200 uint16_t config_reg)
2201{
2202 if (!(config_reg & LPA_SGMII_LINK)) {
2203 state->link = false;
2204 return;
2205 }
2206
2207 switch (config_reg & LPA_SGMII_SPD_MASK) {
2208 case LPA_SGMII_10:
2209 state->speed = SPEED_10;
2210 break;
2211 case LPA_SGMII_100:
2212 state->speed = SPEED_100;
2213 break;
2214 case LPA_SGMII_1000:
2215 state->speed = SPEED_1000;
2216 break;
2217 default:
2218 state->link = false;
2219 return;
2220 }
2221 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2222 state->duplex = DUPLEX_FULL;
2223 else
2224 state->duplex = DUPLEX_HALF;
2225}
2226
2227/**
2228 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2229 * @pcs: a pointer to a &struct mdio_device.
2230 * @state: a pointer to a &struct phylink_link_state.
2231 *
2232 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2233 * clause 37 negotiation and/or SGMII control.
2234 *
2235 * Read the MAC PCS state from the MII device configured in @config and
2236 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2237 * the phylink @state structure. This is suitable to be directly plugged
2238 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2239 * structure.
2240 */
2241void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2242 struct phylink_link_state *state)
2243{
2244 struct mii_bus *bus = pcs->bus;
2245 int addr = pcs->addr;
2246 int bmsr, lpa;
2247
2248 bmsr = mdiobus_read(bus, addr, MII_BMSR);
2249 lpa = mdiobus_read(bus, addr, MII_LPA);
2250 if (bmsr < 0 || lpa < 0) {
2251 state->link = false;
2252 return;
2253 }
2254
2255 state->link = !!(bmsr & BMSR_LSTATUS);
2256 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2257 if (!state->link)
2258 return;
2259
2260 switch (state->interface) {
2261 case PHY_INTERFACE_MODE_1000BASEX:
2262 phylink_decode_c37_word(state, lpa, SPEED_1000);
2263 break;
2264
2265 case PHY_INTERFACE_MODE_2500BASEX:
2266 phylink_decode_c37_word(state, lpa, SPEED_2500);
2267 break;
2268
2269 case PHY_INTERFACE_MODE_SGMII:
2270 phylink_decode_sgmii_word(state, lpa);
2271 break;
2272
2273 default:
2274 state->link = false;
2275 break;
2276 }
2277}
2278EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2279
2280/**
2281 * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2282 * advertisement
2283 * @pcs: a pointer to a &struct mdio_device.
Russell King0bd27402020-03-30 18:44:44 +01002284 * @interface: the PHY interface mode being configured
2285 * @advertising: the ethtool advertisement mask
Russell King74db1c12020-03-17 14:52:36 +00002286 *
2287 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2288 * clause 37 negotiation and/or SGMII control.
2289 *
2290 * Configure the clause 37 PCS advertisement as specified by @state. This
2291 * does not trigger a renegotiation; phylink will do that via the
2292 * mac_an_restart() method of the struct phylink_mac_ops structure.
2293 *
2294 * Returns negative error code on failure to configure the advertisement,
2295 * zero if no change has been made, or one if the advertisement has changed.
2296 */
2297int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
Russell King0bd27402020-03-30 18:44:44 +01002298 phy_interface_t interface,
2299 const unsigned long *advertising)
Russell King74db1c12020-03-17 14:52:36 +00002300{
2301 struct mii_bus *bus = pcs->bus;
2302 int addr = pcs->addr;
2303 int val, ret;
2304 u16 adv;
2305
Russell King0bd27402020-03-30 18:44:44 +01002306 switch (interface) {
Russell King74db1c12020-03-17 14:52:36 +00002307 case PHY_INTERFACE_MODE_1000BASEX:
2308 case PHY_INTERFACE_MODE_2500BASEX:
2309 adv = ADVERTISE_1000XFULL;
2310 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
Russell King0bd27402020-03-30 18:44:44 +01002311 advertising))
Russell King74db1c12020-03-17 14:52:36 +00002312 adv |= ADVERTISE_1000XPAUSE;
2313 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
Russell King0bd27402020-03-30 18:44:44 +01002314 advertising))
Russell King74db1c12020-03-17 14:52:36 +00002315 adv |= ADVERTISE_1000XPSE_ASYM;
2316
2317 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2318 if (val < 0)
2319 return val;
2320
2321 if (val == adv)
2322 return 0;
2323
2324 ret = mdiobus_write(bus, addr, MII_ADVERTISE, adv);
2325 if (ret < 0)
2326 return ret;
2327
2328 return 1;
2329
2330 case PHY_INTERFACE_MODE_SGMII:
2331 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2332 if (val < 0)
2333 return val;
2334
2335 if (val == 0x0001)
2336 return 0;
2337
2338 ret = mdiobus_write(bus, addr, MII_ADVERTISE, 0x0001);
2339 if (ret < 0)
2340 return ret;
2341
2342 return 1;
2343
2344 default:
2345 /* Nothing to do for other modes */
2346 return 0;
2347 }
2348}
2349EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2350
2351/**
2352 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2353 * @pcs: a pointer to a &struct mdio_device.
2354 *
2355 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2356 * clause 37 negotiation.
2357 *
2358 * Restart the clause 37 negotiation with the link partner. This is
2359 * suitable to be directly plugged into the mac_pcs_get_state() member
2360 * of the struct phylink_mac_ops structure.
2361 */
2362void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2363{
2364 struct mii_bus *bus = pcs->bus;
2365 int val, addr = pcs->addr;
2366
2367 val = mdiobus_read(bus, addr, MII_BMCR);
2368 if (val >= 0) {
2369 val |= BMCR_ANRESTART;
2370
2371 mdiobus_write(bus, addr, MII_BMCR, val);
2372 }
2373}
2374EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2375
Russell Kingb8679ef2020-03-17 14:52:41 +00002376void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2377 struct phylink_link_state *state)
2378{
2379 struct mii_bus *bus = pcs->bus;
2380 int addr = pcs->addr;
2381 int stat;
2382
Russell King90ce6652020-05-26 16:29:36 +01002383 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
Russell Kingb8679ef2020-03-17 14:52:41 +00002384 if (stat < 0) {
2385 state->link = false;
2386 return;
2387 }
2388
2389 state->link = !!(stat & MDIO_STAT1_LSTATUS);
2390 if (!state->link)
2391 return;
2392
2393 switch (state->interface) {
2394 case PHY_INTERFACE_MODE_10GBASER:
2395 state->speed = SPEED_10000;
2396 state->duplex = DUPLEX_FULL;
2397 break;
2398
2399 default:
2400 break;
2401 }
2402}
2403EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2404
Andrew Lunn5f857572019-01-21 19:10:19 +01002405MODULE_LICENSE("GPL v2");