blob: 9ac65f435ff1a9d9d4da4419974004bb30d4a817 [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 */
Calvin Johnson25396f62021-06-11 13:53:59 +03008#include <linux/acpi.h>
Russell King9525ae82017-07-25 15:03:13 +01009#include <linux/ethtool.h>
10#include <linux/export.h>
11#include <linux/gpio/consumer.h>
12#include <linux/netdevice.h>
13#include <linux/of.h>
14#include <linux/of_mdio.h>
15#include <linux/phy.h>
16#include <linux/phy_fixed.h>
17#include <linux/phylink.h>
18#include <linux/rtnetlink.h>
19#include <linux/spinlock.h>
Russell King9cd00a82018-05-10 13:17:31 -070020#include <linux/timer.h>
Russell King9525ae82017-07-25 15:03:13 +010021#include <linux/workqueue.h>
22
Russell Kingce0aa272017-07-25 15:03:18 +010023#include "sfp.h"
Russell King9525ae82017-07-25 15:03:13 +010024#include "swphy.h"
25
26#define SUPPORTED_INTERFACES \
27 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
28 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
29#define ADVERTISED_INTERFACES \
30 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
31 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
32
33enum {
34 PHYLINK_DISABLE_STOPPED,
Russell Kingce0aa272017-07-25 15:03:18 +010035 PHYLINK_DISABLE_LINK,
Russell King (Oracle)f9749362021-09-07 18:56:46 +080036 PHYLINK_DISABLE_MAC_WOL,
Russell King9525ae82017-07-25 15:03:13 +010037};
38
Russell King8796c892017-12-01 10:24:47 +000039/**
40 * struct phylink - internal data type for phylink
41 */
Russell King9525ae82017-07-25 15:03:13 +010042struct phylink {
Russell King8796c892017-12-01 10:24:47 +000043 /* private: */
Russell King9525ae82017-07-25 15:03:13 +010044 struct net_device *netdev;
Russell Kinge7765d62020-03-30 18:44:50 +010045 const struct phylink_mac_ops *mac_ops;
Russell King4c0d6d32020-03-30 18:44:55 +010046 const struct phylink_pcs_ops *pcs_ops;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +030047 struct phylink_config *config;
Russell King7137e182020-07-21 12:04:46 +010048 struct phylink_pcs *pcs;
Ioana Ciornei43de6192019-05-28 20:38:13 +030049 struct device *dev;
50 unsigned int old_link_state:1;
Russell King9525ae82017-07-25 15:03:13 +010051
52 unsigned long phylink_disable_state; /* bitmask of disables */
53 struct phy_device *phydev;
54 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
Russell King24cf0e62019-12-11 10:56:35 +000055 u8 cfg_link_an_mode; /* MLO_AN_xxx */
56 u8 cur_link_an_mode;
Russell King9525ae82017-07-25 15:03:13 +010057 u8 link_port; /* The current non-phy ethtool port */
58 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
59
60 /* The link configuration settings */
61 struct phylink_link_state link_config;
Russell Kingc6787262019-05-28 10:27:21 +010062
63 /* The current settings */
64 phy_interface_t cur_interface;
65
Russell King9525ae82017-07-25 15:03:13 +010066 struct gpio_desc *link_gpio;
Russell King7b3b0e82019-05-28 10:57:23 +010067 unsigned int link_irq;
Russell King9cd00a82018-05-10 13:17:31 -070068 struct timer_list link_poll;
Florian Fainelli1ac63e32017-12-12 16:00:28 -080069 void (*get_fixed_state)(struct net_device *dev,
70 struct phylink_link_state *s);
Russell King9525ae82017-07-25 15:03:13 +010071
72 struct mutex state_mutex;
73 struct phylink_link_state phy_state;
74 struct work_struct resolve;
75
76 bool mac_link_dropped;
Russell Kingce0aa272017-07-25 15:03:18 +010077
78 struct sfp_bus *sfp_bus;
Russell King52c95602019-12-11 10:56:45 +000079 bool sfp_may_have_phy;
80 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
81 u8 sfp_port;
Russell King9525ae82017-07-25 15:03:13 +010082};
83
Ioana Ciornei170911802019-05-28 20:38:14 +030084#define phylink_printk(level, pl, fmt, ...) \
85 do { \
86 if ((pl)->config->type == PHYLINK_NETDEV) \
87 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
88 else if ((pl)->config->type == PHYLINK_DEV) \
89 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
90 } while (0)
91
92#define phylink_err(pl, fmt, ...) \
93 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
94#define phylink_warn(pl, fmt, ...) \
95 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
96#define phylink_info(pl, fmt, ...) \
97 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
Florian Fainelli9d68db52019-10-31 15:42:26 -070098#if defined(CONFIG_DYNAMIC_DEBUG)
Ioana Ciornei170911802019-05-28 20:38:14 +030099#define phylink_dbg(pl, fmt, ...) \
Florian Fainelli9d68db52019-10-31 15:42:26 -0700100do { \
101 if ((pl)->config->type == PHYLINK_NETDEV) \
102 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
103 else if ((pl)->config->type == PHYLINK_DEV) \
104 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
105} while (0)
106#elif defined(DEBUG)
107#define phylink_dbg(pl, fmt, ...) \
Ioana Ciornei170911802019-05-28 20:38:14 +0300108 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
Florian Fainelli9d68db52019-10-31 15:42:26 -0700109#else
110#define phylink_dbg(pl, fmt, ...) \
111({ \
112 if (0) \
113 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
114})
115#endif
Ioana Ciornei170911802019-05-28 20:38:14 +0300116
Russell King8796c892017-12-01 10:24:47 +0000117/**
118 * phylink_set_port_modes() - set the port type modes in the ethtool mask
119 * @mask: ethtool link mode mask
120 *
121 * Sets all the port type modes in the ethtool mask. MAC drivers should
122 * use this in their 'validate' callback.
123 */
Russell King9525ae82017-07-25 15:03:13 +0100124void phylink_set_port_modes(unsigned long *mask)
125{
126 phylink_set(mask, TP);
127 phylink_set(mask, AUI);
128 phylink_set(mask, MII);
129 phylink_set(mask, FIBRE);
130 phylink_set(mask, BNC);
131 phylink_set(mask, Backplane);
132}
133EXPORT_SYMBOL_GPL(phylink_set_port_modes);
134
Russell King (Oracle)a2c27a62021-10-04 12:03:28 +0100135void phylink_set_10g_modes(unsigned long *mask)
136{
137 phylink_set(mask, 10000baseT_Full);
138 phylink_set(mask, 10000baseCR_Full);
139 phylink_set(mask, 10000baseSR_Full);
140 phylink_set(mask, 10000baseLR_Full);
141 phylink_set(mask, 10000baseLRM_Full);
142 phylink_set(mask, 10000baseER_Full);
143}
144EXPORT_SYMBOL_GPL(phylink_set_10g_modes);
145
Russell King9525ae82017-07-25 15:03:13 +0100146static int phylink_is_empty_linkmode(const unsigned long *linkmode)
147{
148 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
149
150 phylink_set_port_modes(tmp);
151 phylink_set(tmp, Autoneg);
152 phylink_set(tmp, Pause);
153 phylink_set(tmp, Asym_Pause);
154
Russell King554032c2019-10-15 11:28:46 +0100155 return linkmode_subset(linkmode, tmp);
Russell King9525ae82017-07-25 15:03:13 +0100156}
157
158static const char *phylink_an_mode_str(unsigned int mode)
159{
160 static const char *modestr[] = {
161 [MLO_AN_PHY] = "phy",
162 [MLO_AN_FIXED] = "fixed",
Russell King86a362c2017-12-01 10:24:26 +0000163 [MLO_AN_INBAND] = "inband",
Russell King9525ae82017-07-25 15:03:13 +0100164 };
165
166 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
167}
168
169static int phylink_validate(struct phylink *pl, unsigned long *supported,
170 struct phylink_link_state *state)
171{
Russell Kinge7765d62020-03-30 18:44:50 +0100172 pl->mac_ops->validate(pl->config, supported, state);
Russell King9525ae82017-07-25 15:03:13 +0100173
174 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
175}
176
Russell King8fa7b9b2017-12-01 10:25:09 +0000177static int phylink_parse_fixedlink(struct phylink *pl,
178 struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100179{
Russell King8fa7b9b2017-12-01 10:25:09 +0000180 struct fwnode_handle *fixed_node;
Russell King9525ae82017-07-25 15:03:13 +0100181 const struct phy_setting *s;
182 struct gpio_desc *desc;
Russell King9525ae82017-07-25 15:03:13 +0100183 u32 speed;
Russell King8fa7b9b2017-12-01 10:25:09 +0000184 int ret;
Russell King9525ae82017-07-25 15:03:13 +0100185
Russell King8fa7b9b2017-12-01 10:25:09 +0000186 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
Russell King9525ae82017-07-25 15:03:13 +0100187 if (fixed_node) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000188 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
Russell King9525ae82017-07-25 15:03:13 +0100189
190 pl->link_config.speed = speed;
191 pl->link_config.duplex = DUPLEX_HALF;
192
Russell King8fa7b9b2017-12-01 10:25:09 +0000193 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
Russell King9525ae82017-07-25 15:03:13 +0100194 pl->link_config.duplex = DUPLEX_FULL;
195
196 /* We treat the "pause" and "asym-pause" terminology as
Wenpeng Liang1953feb2021-06-16 18:01:20 +0800197 * defining the link partner's ability.
198 */
Russell King8fa7b9b2017-12-01 10:25:09 +0000199 if (fwnode_property_read_bool(fixed_node, "pause"))
Russell King4e5aeb42020-02-15 15:49:53 +0000200 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
201 pl->link_config.lp_advertising);
Russell King8fa7b9b2017-12-01 10:25:09 +0000202 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
Russell King4e5aeb42020-02-15 15:49:53 +0000203 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
204 pl->link_config.lp_advertising);
Russell King9525ae82017-07-25 15:03:13 +0100205
206 if (ret == 0) {
Dmitry Torokhovb605c9a2020-01-02 17:03:18 -0800207 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
208 GPIOD_IN, "?");
Russell King9525ae82017-07-25 15:03:13 +0100209
210 if (!IS_ERR(desc))
211 pl->link_gpio = desc;
212 else if (desc == ERR_PTR(-EPROBE_DEFER))
213 ret = -EPROBE_DEFER;
214 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000215 fwnode_handle_put(fixed_node);
Russell King9525ae82017-07-25 15:03:13 +0100216
217 if (ret)
218 return ret;
219 } else {
Russell King8fa7b9b2017-12-01 10:25:09 +0000220 u32 prop[5];
221
222 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
223 NULL, 0);
224 if (ret != ARRAY_SIZE(prop)) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300225 phylink_err(pl, "broken fixed-link?\n");
Russell King9525ae82017-07-25 15:03:13 +0100226 return -EINVAL;
227 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000228
229 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
230 prop, ARRAY_SIZE(prop));
231 if (!ret) {
232 pl->link_config.duplex = prop[1] ?
Russell King9525ae82017-07-25 15:03:13 +0100233 DUPLEX_FULL : DUPLEX_HALF;
Russell King8fa7b9b2017-12-01 10:25:09 +0000234 pl->link_config.speed = prop[2];
235 if (prop[3])
Russell King4e5aeb42020-02-15 15:49:53 +0000236 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
237 pl->link_config.lp_advertising);
Russell King8fa7b9b2017-12-01 10:25:09 +0000238 if (prop[4])
Russell King4e5aeb42020-02-15 15:49:53 +0000239 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
240 pl->link_config.lp_advertising);
Russell King9525ae82017-07-25 15:03:13 +0100241 }
242 }
243
244 if (pl->link_config.speed > SPEED_1000 &&
245 pl->link_config.duplex != DUPLEX_FULL)
Ioana Ciornei170911802019-05-28 20:38:14 +0300246 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
247 pl->link_config.speed);
Russell King9525ae82017-07-25 15:03:13 +0100248
249 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
250 linkmode_copy(pl->link_config.advertising, pl->supported);
251 phylink_validate(pl, pl->supported, &pl->link_config);
252
253 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100254 pl->supported, true);
Russell King9525ae82017-07-25 15:03:13 +0100255 linkmode_zero(pl->supported);
256 phylink_set(pl->supported, MII);
René van Dorst8aace4f2019-07-27 11:40:11 +0200257 phylink_set(pl->supported, Pause);
258 phylink_set(pl->supported, Asym_Pause);
Russell King1ceb7ee2020-07-21 12:03:45 +0100259 phylink_set(pl->supported, Autoneg);
Russell King9525ae82017-07-25 15:03:13 +0100260 if (s) {
261 __set_bit(s->bit, pl->supported);
Russell King1ceb7ee2020-07-21 12:03:45 +0100262 __set_bit(s->bit, pl->link_config.lp_advertising);
Russell King9525ae82017-07-25 15:03:13 +0100263 } else {
Ioana Ciornei170911802019-05-28 20:38:14 +0300264 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
265 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
266 pl->link_config.speed);
Russell King9525ae82017-07-25 15:03:13 +0100267 }
268
269 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
270 pl->supported);
271
272 pl->link_config.link = 1;
273 pl->link_config.an_complete = 1;
274
275 return 0;
276}
277
Russell King8fa7b9b2017-12-01 10:25:09 +0000278static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100279{
Russell King8fa7b9b2017-12-01 10:25:09 +0000280 struct fwnode_handle *dn;
Russell King9525ae82017-07-25 15:03:13 +0100281 const char *managed;
282
Russell King8fa7b9b2017-12-01 10:25:09 +0000283 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
284 if (dn || fwnode_property_present(fwnode, "fixed-link"))
Russell King24cf0e62019-12-11 10:56:35 +0000285 pl->cfg_link_an_mode = MLO_AN_FIXED;
Russell King8fa7b9b2017-12-01 10:25:09 +0000286 fwnode_handle_put(dn);
Russell King9525ae82017-07-25 15:03:13 +0100287
Ong Boon Leongab393852021-03-15 13:27:08 +0800288 if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
289 strcmp(managed, "in-band-status") == 0) ||
290 pl->config->ovr_an_inband) {
Russell King24cf0e62019-12-11 10:56:35 +0000291 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300292 phylink_err(pl,
293 "can't use both fixed-link and in-band-status\n");
Russell King9525ae82017-07-25 15:03:13 +0100294 return -EINVAL;
295 }
296
297 linkmode_zero(pl->supported);
298 phylink_set(pl->supported, MII);
299 phylink_set(pl->supported, Autoneg);
300 phylink_set(pl->supported, Asym_Pause);
301 phylink_set(pl->supported, Pause);
302 pl->link_config.an_enabled = true;
Russell King24cf0e62019-12-11 10:56:35 +0000303 pl->cfg_link_an_mode = MLO_AN_INBAND;
Russell King9525ae82017-07-25 15:03:13 +0100304
305 switch (pl->link_config.interface) {
306 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean3a68ba62020-01-06 03:34:10 +0200307 case PHY_INTERFACE_MODE_QSGMII:
Russell King9525ae82017-07-25 15:03:13 +0100308 phylink_set(pl->supported, 10baseT_Half);
309 phylink_set(pl->supported, 10baseT_Full);
310 phylink_set(pl->supported, 100baseT_Half);
311 phylink_set(pl->supported, 100baseT_Full);
312 phylink_set(pl->supported, 1000baseT_Half);
313 phylink_set(pl->supported, 1000baseT_Full);
Russell King9525ae82017-07-25 15:03:13 +0100314 break;
315
316 case PHY_INTERFACE_MODE_1000BASEX:
317 phylink_set(pl->supported, 1000baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100318 break;
319
320 case PHY_INTERFACE_MODE_2500BASEX:
321 phylink_set(pl->supported, 2500baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100322 break;
323
Marek Behúnf6813bd2021-02-16 20:20:54 +0100324 case PHY_INTERFACE_MODE_5GBASER:
325 phylink_set(pl->supported, 5000baseT_Full);
326 break;
327
Steen Hegelund21e0c592021-06-11 14:54:53 +0200328 case PHY_INTERFACE_MODE_25GBASER:
329 phylink_set(pl->supported, 25000baseCR_Full);
330 phylink_set(pl->supported, 25000baseKR_Full);
331 phylink_set(pl->supported, 25000baseSR_Full);
332 fallthrough;
Alex Marginean04e22462020-01-18 14:19:15 +0200333 case PHY_INTERFACE_MODE_USXGMII:
Russell Kingda7c1862017-07-25 15:03:34 +0100334 case PHY_INTERFACE_MODE_10GKR:
Russell Kinge0f909b2020-01-03 20:43:23 +0000335 case PHY_INTERFACE_MODE_10GBASER:
Russell Kingda7c1862017-07-25 15:03:34 +0100336 phylink_set(pl->supported, 10baseT_Half);
337 phylink_set(pl->supported, 10baseT_Full);
338 phylink_set(pl->supported, 100baseT_Half);
339 phylink_set(pl->supported, 100baseT_Full);
340 phylink_set(pl->supported, 1000baseT_Half);
341 phylink_set(pl->supported, 1000baseT_Full);
342 phylink_set(pl->supported, 1000baseX_Full);
Jose Abreuc5801652020-03-09 09:36:24 +0100343 phylink_set(pl->supported, 1000baseKX_Full);
Vladimir Oltean6cbdcf22020-01-16 19:36:56 +0200344 phylink_set(pl->supported, 2500baseT_Full);
345 phylink_set(pl->supported, 2500baseX_Full);
346 phylink_set(pl->supported, 5000baseT_Full);
347 phylink_set(pl->supported, 10000baseT_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100348 phylink_set(pl->supported, 10000baseKR_Full);
Jose Abreuc5801652020-03-09 09:36:24 +0100349 phylink_set(pl->supported, 10000baseKX4_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100350 phylink_set(pl->supported, 10000baseCR_Full);
351 phylink_set(pl->supported, 10000baseSR_Full);
352 phylink_set(pl->supported, 10000baseLR_Full);
353 phylink_set(pl->supported, 10000baseLRM_Full);
354 phylink_set(pl->supported, 10000baseER_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100355 break;
356
Jose Abreu1671c422020-03-12 18:10:10 +0100357 case PHY_INTERFACE_MODE_XLGMII:
358 phylink_set(pl->supported, 25000baseCR_Full);
359 phylink_set(pl->supported, 25000baseKR_Full);
360 phylink_set(pl->supported, 25000baseSR_Full);
361 phylink_set(pl->supported, 40000baseKR4_Full);
362 phylink_set(pl->supported, 40000baseCR4_Full);
363 phylink_set(pl->supported, 40000baseSR4_Full);
364 phylink_set(pl->supported, 40000baseLR4_Full);
365 phylink_set(pl->supported, 50000baseCR2_Full);
366 phylink_set(pl->supported, 50000baseKR2_Full);
367 phylink_set(pl->supported, 50000baseSR2_Full);
368 phylink_set(pl->supported, 50000baseKR_Full);
369 phylink_set(pl->supported, 50000baseSR_Full);
370 phylink_set(pl->supported, 50000baseCR_Full);
371 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
372 phylink_set(pl->supported, 50000baseDR_Full);
373 phylink_set(pl->supported, 100000baseKR4_Full);
374 phylink_set(pl->supported, 100000baseSR4_Full);
375 phylink_set(pl->supported, 100000baseCR4_Full);
376 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
377 phylink_set(pl->supported, 100000baseKR2_Full);
378 phylink_set(pl->supported, 100000baseSR2_Full);
379 phylink_set(pl->supported, 100000baseCR2_Full);
380 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
381 phylink_set(pl->supported, 100000baseDR2_Full);
382 break;
383
Russell King9525ae82017-07-25 15:03:13 +0100384 default:
Ioana Ciornei170911802019-05-28 20:38:14 +0300385 phylink_err(pl,
386 "incorrect link mode %s for in-band status\n",
387 phy_modes(pl->link_config.interface));
Russell King9525ae82017-07-25 15:03:13 +0100388 return -EINVAL;
389 }
390
391 linkmode_copy(pl->link_config.advertising, pl->supported);
392
393 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300394 phylink_err(pl,
395 "failed to validate link configuration for in-band status\n");
Russell King9525ae82017-07-25 15:03:13 +0100396 return -EINVAL;
397 }
Jose Abreu94148192020-03-09 09:36:25 +0100398
399 /* Check if MAC/PCS also supports Autoneg. */
400 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
Russell King9525ae82017-07-25 15:03:13 +0100401 }
402
403 return 0;
404}
405
Russell King2d5fbef2020-02-15 15:49:43 +0000406static void phylink_apply_manual_flow(struct phylink *pl,
407 struct phylink_link_state *state)
408{
409 /* If autoneg is disabled, pause AN is also disabled */
410 if (!state->an_enabled)
411 state->pause &= ~MLO_PAUSE_AN;
412
413 /* Manual configuration of pause modes */
414 if (!(pl->link_config.pause & MLO_PAUSE_AN))
415 state->pause = pl->link_config.pause;
416}
417
Russell King4e5aeb42020-02-15 15:49:53 +0000418static void phylink_resolve_flow(struct phylink_link_state *state)
419{
420 bool tx_pause, rx_pause;
421
422 state->pause = MLO_PAUSE_NONE;
423 if (state->duplex == DUPLEX_FULL) {
424 linkmode_resolve_pause(state->advertising,
425 state->lp_advertising,
426 &tx_pause, &rx_pause);
427 if (tx_pause)
428 state->pause |= MLO_PAUSE_TX;
429 if (rx_pause)
430 state->pause |= MLO_PAUSE_RX;
431 }
432}
433
Russell King9525ae82017-07-25 15:03:13 +0100434static void phylink_mac_config(struct phylink *pl,
435 const struct phylink_link_state *state)
436{
Ioana Ciornei170911802019-05-28 20:38:14 +0300437 phylink_dbg(pl,
438 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
Russell King24cf0e62019-12-11 10:56:35 +0000439 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
Ioana Ciornei170911802019-05-28 20:38:14 +0300440 phy_modes(state->interface),
441 phy_speed_to_str(state->speed),
442 phy_duplex_to_str(state->duplex),
443 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
444 state->pause, state->link, state->an_enabled);
Russell King9525ae82017-07-25 15:03:13 +0100445
Russell Kinge7765d62020-03-30 18:44:50 +0100446 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
Russell King9525ae82017-07-25 15:03:13 +0100447}
448
Russell King4c0d6d32020-03-30 18:44:55 +0100449static void phylink_mac_pcs_an_restart(struct phylink *pl)
Russell King9525ae82017-07-25 15:03:13 +0100450{
451 if (pl->link_config.an_enabled &&
Russell King575691b2020-06-24 12:30:04 +0100452 phy_interface_mode_is_8023z(pl->link_config.interface) &&
453 phylink_autoneg_inband(pl->cur_link_an_mode)) {
Russell King4c0d6d32020-03-30 18:44:55 +0100454 if (pl->pcs_ops)
Russell King7137e182020-07-21 12:04:46 +0100455 pl->pcs_ops->pcs_an_restart(pl->pcs);
Russell King4c0d6d32020-03-30 18:44:55 +0100456 else
457 pl->mac_ops->mac_an_restart(pl->config);
458 }
459}
460
Russell Kingb7ad14c2020-07-21 12:04:41 +0100461static void phylink_major_config(struct phylink *pl, bool restart,
462 const struct phylink_link_state *state)
Russell King4c0d6d32020-03-30 18:44:55 +0100463{
Russell Kingb7ad14c2020-07-21 12:04:41 +0100464 int err;
Russell King4c0d6d32020-03-30 18:44:55 +0100465
Russell Kingb7ad14c2020-07-21 12:04:41 +0100466 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
467
468 if (pl->mac_ops->mac_prepare) {
469 err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
470 state->interface);
471 if (err < 0) {
472 phylink_err(pl, "mac_prepare failed: %pe\n",
473 ERR_PTR(err));
474 return;
475 }
476 }
Russell King4c0d6d32020-03-30 18:44:55 +0100477
478 phylink_mac_config(pl, state);
479
Russell Kingb7ad14c2020-07-21 12:04:41 +0100480 if (pl->pcs_ops) {
Russell King7137e182020-07-21 12:04:46 +0100481 err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
Russell Kingb7ad14c2020-07-21 12:04:41 +0100482 state->interface,
483 state->advertising,
484 !!(pl->link_config.pause &
485 MLO_PAUSE_AN));
486 if (err < 0)
487 phylink_err(pl, "pcs_config failed: %pe\n",
488 ERR_PTR(err));
489 if (err > 0)
490 restart = true;
491 }
Russell King4c0d6d32020-03-30 18:44:55 +0100492 if (restart)
493 phylink_mac_pcs_an_restart(pl);
Russell Kingb7ad14c2020-07-21 12:04:41 +0100494
495 if (pl->mac_ops->mac_finish) {
496 err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
497 state->interface);
498 if (err < 0)
Ong Boon Leongd82c6c12021-03-15 12:33:42 +0800499 phylink_err(pl, "mac_finish failed: %pe\n",
Russell Kingb7ad14c2020-07-21 12:04:41 +0100500 ERR_PTR(err));
501 }
Russell King9525ae82017-07-25 15:03:13 +0100502}
503
Russell King1571e702020-07-21 12:04:36 +0100504/*
505 * Reconfigure for a change of inband advertisement.
506 * If we have a separate PCS, we only need to call its pcs_config() method,
507 * and then restart AN if it indicates something changed. Otherwise, we do
508 * the full MAC reconfiguration.
509 */
510static int phylink_change_inband_advert(struct phylink *pl)
511{
512 int ret;
513
514 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
515 return 0;
516
517 if (!pl->pcs_ops) {
518 /* Legacy method */
519 phylink_mac_config(pl, &pl->link_config);
520 phylink_mac_pcs_an_restart(pl);
521 return 0;
522 }
523
524 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
525 phylink_an_mode_str(pl->cur_link_an_mode),
526 phy_modes(pl->link_config.interface),
527 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
528 pl->link_config.pause);
529
530 /* Modern PCS-based method; update the advert at the PCS, and
531 * restart negotiation if the pcs_config() helper indicates that
532 * the programmed advertisement has changed.
533 */
Russell King7137e182020-07-21 12:04:46 +0100534 ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
Russell King1571e702020-07-21 12:04:36 +0100535 pl->link_config.interface,
536 pl->link_config.advertising,
537 !!(pl->link_config.pause & MLO_PAUSE_AN));
538 if (ret < 0)
539 return ret;
540
541 if (ret > 0)
542 phylink_mac_pcs_an_restart(pl);
543
544 return 0;
545}
546
Russell Kingd46b7e42019-11-21 00:36:22 +0000547static void phylink_mac_pcs_get_state(struct phylink *pl,
548 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100549{
Russell King9525ae82017-07-25 15:03:13 +0100550 linkmode_copy(state->advertising, pl->link_config.advertising);
551 linkmode_zero(state->lp_advertising);
552 state->interface = pl->link_config.interface;
553 state->an_enabled = pl->link_config.an_enabled;
Heiner Kallweitd25ed412019-02-26 19:29:22 +0100554 state->speed = SPEED_UNKNOWN;
555 state->duplex = DUPLEX_UNKNOWN;
556 state->pause = MLO_PAUSE_NONE;
557 state->an_complete = 0;
Russell King9525ae82017-07-25 15:03:13 +0100558 state->link = 1;
559
Russell King4c0d6d32020-03-30 18:44:55 +0100560 if (pl->pcs_ops)
Russell King7137e182020-07-21 12:04:46 +0100561 pl->pcs_ops->pcs_get_state(pl->pcs, state);
Russell Kinge859a602020-08-28 11:53:53 +0100562 else if (pl->mac_ops->mac_pcs_get_state)
Russell King4c0d6d32020-03-30 18:44:55 +0100563 pl->mac_ops->mac_pcs_get_state(pl->config, state);
Russell Kinge859a602020-08-28 11:53:53 +0100564 else
565 state->link = 0;
Russell King9525ae82017-07-25 15:03:13 +0100566}
567
568/* The fixed state is... fixed except for the link state,
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800569 * which may be determined by a GPIO or a callback.
Russell King9525ae82017-07-25 15:03:13 +0100570 */
Russell King4e5aeb42020-02-15 15:49:53 +0000571static void phylink_get_fixed_state(struct phylink *pl,
572 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100573{
574 *state = pl->link_config;
Russell King5c05c1d2020-04-23 17:02:56 +0100575 if (pl->config->get_fixed_state)
576 pl->config->get_fixed_state(pl->config, state);
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800577 else if (pl->link_gpio)
Florian Fainellibb322a92018-05-10 13:17:29 -0700578 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
Russell King9525ae82017-07-25 15:03:13 +0100579
Russell King4e5aeb42020-02-15 15:49:53 +0000580 phylink_resolve_flow(state);
Russell King9525ae82017-07-25 15:03:13 +0100581}
582
Russell King4c0d6d32020-03-30 18:44:55 +0100583static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
Russell King97fec512020-02-15 15:50:03 +0000584{
585 struct phylink_link_state link_state;
586
587 switch (pl->cur_link_an_mode) {
588 case MLO_AN_PHY:
589 link_state = pl->phy_state;
590 break;
591
592 case MLO_AN_FIXED:
593 phylink_get_fixed_state(pl, &link_state);
594 break;
595
596 case MLO_AN_INBAND:
597 link_state = pl->link_config;
598 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
599 link_state.pause = MLO_PAUSE_NONE;
600 break;
601
602 default: /* can't happen */
603 return;
604 }
605
606 link_state.link = false;
607
608 phylink_apply_manual_flow(pl, &link_state);
Russell Kingb7ad14c2020-07-21 12:04:41 +0100609 phylink_major_config(pl, force_restart, &link_state);
Russell King97fec512020-02-15 15:50:03 +0000610}
611
Russell King9525ae82017-07-25 15:03:13 +0100612static const char *phylink_pause_to_str(int pause)
613{
614 switch (pause & MLO_PAUSE_TXRX_MASK) {
615 case MLO_PAUSE_TX | MLO_PAUSE_RX:
616 return "rx/tx";
617 case MLO_PAUSE_TX:
618 return "tx";
619 case MLO_PAUSE_RX:
620 return "rx";
621 default:
622 return "off";
623 }
624}
625
Russell King4c0d6d32020-03-30 18:44:55 +0100626static void phylink_link_up(struct phylink *pl,
627 struct phylink_link_state link_state)
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300628{
629 struct net_device *ndev = pl->netdev;
630
David S. Millerb4b12b02019-05-31 10:49:43 -0700631 pl->cur_interface = link_state.interface;
Russell King4c0d6d32020-03-30 18:44:55 +0100632
633 if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
Russell King7137e182020-07-21 12:04:46 +0100634 pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
Russell King4c0d6d32020-03-30 18:44:55 +0100635 pl->cur_interface,
636 link_state.speed, link_state.duplex);
637
Russell Kinge7765d62020-03-30 18:44:50 +0100638 pl->mac_ops->mac_link_up(pl->config, pl->phydev,
639 pl->cur_link_an_mode, pl->cur_interface,
640 link_state.speed, link_state.duplex,
641 !!(link_state.pause & MLO_PAUSE_TX),
642 !!(link_state.pause & MLO_PAUSE_RX));
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300643
Ioana Ciornei43de6192019-05-28 20:38:13 +0300644 if (ndev)
645 netif_carrier_on(ndev);
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300646
Ioana Ciornei170911802019-05-28 20:38:14 +0300647 phylink_info(pl,
648 "Link is Up - %s/%s - flow control %s\n",
649 phy_speed_to_str(link_state.speed),
650 phy_duplex_to_str(link_state.duplex),
651 phylink_pause_to_str(link_state.pause));
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300652}
653
Russell King4c0d6d32020-03-30 18:44:55 +0100654static void phylink_link_down(struct phylink *pl)
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300655{
656 struct net_device *ndev = pl->netdev;
657
Ioana Ciornei43de6192019-05-28 20:38:13 +0300658 if (ndev)
659 netif_carrier_off(ndev);
Russell Kinge7765d62020-03-30 18:44:50 +0100660 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
661 pl->cur_interface);
Ioana Ciornei170911802019-05-28 20:38:14 +0300662 phylink_info(pl, "Link is Down\n");
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300663}
664
Russell King9525ae82017-07-25 15:03:13 +0100665static void phylink_resolve(struct work_struct *w)
666{
667 struct phylink *pl = container_of(w, struct phylink, resolve);
668 struct phylink_link_state link_state;
669 struct net_device *ndev = pl->netdev;
Russell King319bfaf2020-07-21 12:03:55 +0100670 bool mac_config = false;
Russell Kingb06e5ca2020-07-21 12:03:50 +0100671 bool cur_link_state;
Russell King9525ae82017-07-25 15:03:13 +0100672
673 mutex_lock(&pl->state_mutex);
Russell Kingb06e5ca2020-07-21 12:03:50 +0100674 if (pl->netdev)
675 cur_link_state = netif_carrier_ok(ndev);
676 else
677 cur_link_state = pl->old_link_state;
678
Russell King9525ae82017-07-25 15:03:13 +0100679 if (pl->phylink_disable_state) {
680 pl->mac_link_dropped = false;
681 link_state.link = false;
682 } else if (pl->mac_link_dropped) {
683 link_state.link = false;
684 } else {
Russell King24cf0e62019-12-11 10:56:35 +0000685 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +0100686 case MLO_AN_PHY:
687 link_state = pl->phy_state;
Russell King2d5fbef2020-02-15 15:49:43 +0000688 phylink_apply_manual_flow(pl, &link_state);
Russell King319bfaf2020-07-21 12:03:55 +0100689 mac_config = link_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100690 break;
691
692 case MLO_AN_FIXED:
693 phylink_get_fixed_state(pl, &link_state);
Russell King319bfaf2020-07-21 12:03:55 +0100694 mac_config = link_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100695 break;
696
Russell King86a362c2017-12-01 10:24:26 +0000697 case MLO_AN_INBAND:
Russell Kingd46b7e42019-11-21 00:36:22 +0000698 phylink_mac_pcs_get_state(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100699
Russell King406cb0c2019-05-20 16:07:20 +0100700 /* If we have a phy, the "up" state is the union of
Wenpeng Liang1953feb2021-06-16 18:01:20 +0800701 * both the PHY and the MAC
702 */
Russell King406cb0c2019-05-20 16:07:20 +0100703 if (pl->phydev)
704 link_state.link &= pl->phy_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100705
Russell King406cb0c2019-05-20 16:07:20 +0100706 /* Only update if the PHY link is up */
707 if (pl->phydev && pl->phy_state.link) {
708 link_state.interface = pl->phy_state.interface;
Russell King9525ae82017-07-25 15:03:13 +0100709
Russell King406cb0c2019-05-20 16:07:20 +0100710 /* If we have a PHY, we need to update with
Wenpeng Liang1953feb2021-06-16 18:01:20 +0800711 * the PHY flow control bits.
712 */
Russell King33faac82020-02-15 15:49:48 +0000713 link_state.pause = pl->phy_state.pause;
Russell King319bfaf2020-07-21 12:03:55 +0100714 mac_config = true;
Russell King9525ae82017-07-25 15:03:13 +0100715 }
Russell King319bfaf2020-07-21 12:03:55 +0100716 phylink_apply_manual_flow(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100717 break;
Russell King9525ae82017-07-25 15:03:13 +0100718 }
719 }
720
Russell King16319a72020-07-21 12:04:00 +0100721 if (mac_config) {
722 if (link_state.interface != pl->link_config.interface) {
723 /* The interface has changed, force the link down and
724 * then reconfigure.
725 */
726 if (cur_link_state) {
727 phylink_link_down(pl);
728 cur_link_state = false;
729 }
Russell Kingb7ad14c2020-07-21 12:04:41 +0100730 phylink_major_config(pl, false, &link_state);
Russell King5005b162020-07-21 12:04:05 +0100731 pl->link_config.interface = link_state.interface;
Russell King7cceb592020-07-21 12:04:10 +0100732 } else if (!pl->pcs_ops) {
Russell King5005b162020-07-21 12:04:05 +0100733 /* The interface remains unchanged, only the speed,
734 * duplex or pause settings have changed. Call the
Russell King7cceb592020-07-21 12:04:10 +0100735 * old mac_config() method to configure the MAC/PCS
736 * only if we do not have a PCS installed (an
737 * unconverted user.)
Russell King5005b162020-07-21 12:04:05 +0100738 */
739 phylink_mac_config(pl, &link_state);
Russell King16319a72020-07-21 12:04:00 +0100740 }
Russell King16319a72020-07-21 12:04:00 +0100741 }
Russell King319bfaf2020-07-21 12:03:55 +0100742
Russell Kingb06e5ca2020-07-21 12:03:50 +0100743 if (link_state.link != cur_link_state) {
Ioana Ciornei43de6192019-05-28 20:38:13 +0300744 pl->old_link_state = link_state.link;
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300745 if (!link_state.link)
Russell King4c0d6d32020-03-30 18:44:55 +0100746 phylink_link_down(pl);
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300747 else
Russell King4c0d6d32020-03-30 18:44:55 +0100748 phylink_link_up(pl, link_state);
Russell King9525ae82017-07-25 15:03:13 +0100749 }
750 if (!link_state.link && pl->mac_link_dropped) {
751 pl->mac_link_dropped = false;
752 queue_work(system_power_efficient_wq, &pl->resolve);
753 }
754 mutex_unlock(&pl->state_mutex);
755}
756
757static void phylink_run_resolve(struct phylink *pl)
758{
759 if (!pl->phylink_disable_state)
760 queue_work(system_power_efficient_wq, &pl->resolve);
761}
762
Russell King87454b62019-02-11 15:04:24 +0000763static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
764{
765 unsigned long state = pl->phylink_disable_state;
766
767 set_bit(bit, &pl->phylink_disable_state);
768 if (state == 0) {
769 queue_work(system_power_efficient_wq, &pl->resolve);
770 flush_work(&pl->resolve);
771 }
772}
773
Russell King9cd00a82018-05-10 13:17:31 -0700774static void phylink_fixed_poll(struct timer_list *t)
775{
776 struct phylink *pl = container_of(t, struct phylink, link_poll);
777
778 mod_timer(t, jiffies + HZ);
779
780 phylink_run_resolve(pl);
781}
782
Russell Kingce0aa272017-07-25 15:03:18 +0100783static const struct sfp_upstream_ops sfp_phylink_ops;
784
Russell King8fa7b9b2017-12-01 10:25:09 +0000785static int phylink_register_sfp(struct phylink *pl,
786 struct fwnode_handle *fwnode)
Russell Kingce0aa272017-07-25 15:03:18 +0100787{
Russell King2203cbf2019-10-15 11:38:39 +0100788 struct sfp_bus *bus;
Russell King8fa7b9b2017-12-01 10:25:09 +0000789 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100790
Russell Kingeed70fd2020-01-03 15:13:56 +0000791 if (!fwnode)
792 return 0;
793
Russell King727b3662019-11-08 17:39:29 +0000794 bus = sfp_bus_find_fwnode(fwnode);
Russell King2203cbf2019-10-15 11:38:39 +0100795 if (IS_ERR(bus)) {
796 ret = PTR_ERR(bus);
797 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
Russell King8fa7b9b2017-12-01 10:25:09 +0000798 return ret;
799 }
800
Russell King2203cbf2019-10-15 11:38:39 +0100801 pl->sfp_bus = bus;
Russell Kingce0aa272017-07-25 15:03:18 +0100802
Russell King727b3662019-11-08 17:39:29 +0000803 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
804 sfp_bus_put(bus);
805
806 return ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100807}
808
Russell King8796c892017-12-01 10:24:47 +0000809/**
810 * phylink_create() - create a phylink instance
Randy Dunlap9db74e52019-10-08 08:39:04 -0700811 * @config: a pointer to the target &struct phylink_config
Russell King8fa7b9b2017-12-01 10:25:09 +0000812 * @fwnode: a pointer to a &struct fwnode_handle describing the network
813 * interface
Russell King8796c892017-12-01 10:24:47 +0000814 * @iface: the desired link mode defined by &typedef phy_interface_t
Russell Kinge7765d62020-03-30 18:44:50 +0100815 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
Russell King8796c892017-12-01 10:24:47 +0000816 *
817 * Create a new phylink instance, and parse the link parameters found in @np.
818 * This will parse in-band modes, fixed-link or SFP configuration.
819 *
Russell King269a6b52019-11-19 17:18:52 +0000820 * Note: the rtnl lock must not be held when calling this function.
821 *
Russell King8796c892017-12-01 10:24:47 +0000822 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
823 * must use IS_ERR() to check for errors from this function.
824 */
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300825struct phylink *phylink_create(struct phylink_config *config,
Russell King8fa7b9b2017-12-01 10:25:09 +0000826 struct fwnode_handle *fwnode,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700827 phy_interface_t iface,
Russell Kinge7765d62020-03-30 18:44:50 +0100828 const struct phylink_mac_ops *mac_ops)
Russell King9525ae82017-07-25 15:03:13 +0100829{
830 struct phylink *pl;
831 int ret;
832
833 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
834 if (!pl)
835 return ERR_PTR(-ENOMEM);
836
837 mutex_init(&pl->state_mutex);
838 INIT_WORK(&pl->resolve, phylink_resolve);
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300839
840 pl->config = config;
841 if (config->type == PHYLINK_NETDEV) {
842 pl->netdev = to_net_dev(config->dev);
Ioana Ciornei43de6192019-05-28 20:38:13 +0300843 } else if (config->type == PHYLINK_DEV) {
844 pl->dev = config->dev;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300845 } else {
846 kfree(pl);
847 return ERR_PTR(-EINVAL);
848 }
849
Russell King9525ae82017-07-25 15:03:13 +0100850 pl->phy_state.interface = iface;
851 pl->link_interface = iface;
Florian Fainelli4be11ef2017-12-12 16:00:29 -0800852 if (iface == PHY_INTERFACE_MODE_MOCA)
853 pl->link_port = PORT_BNC;
854 else
855 pl->link_port = PORT_MII;
Russell King9525ae82017-07-25 15:03:13 +0100856 pl->link_config.interface = iface;
857 pl->link_config.pause = MLO_PAUSE_AN;
858 pl->link_config.speed = SPEED_UNKNOWN;
859 pl->link_config.duplex = DUPLEX_UNKNOWN;
Russell King74ee0e82017-12-20 23:21:34 +0000860 pl->link_config.an_enabled = true;
Russell Kinge7765d62020-03-30 18:44:50 +0100861 pl->mac_ops = mac_ops;
Russell King9525ae82017-07-25 15:03:13 +0100862 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King9cd00a82018-05-10 13:17:31 -0700863 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
Russell King9525ae82017-07-25 15:03:13 +0100864
865 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
866 linkmode_copy(pl->link_config.advertising, pl->supported);
867 phylink_validate(pl, pl->supported, &pl->link_config);
868
Russell King8fa7b9b2017-12-01 10:25:09 +0000869 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100870 if (ret < 0) {
871 kfree(pl);
872 return ERR_PTR(ret);
873 }
874
Russell King24cf0e62019-12-11 10:56:35 +0000875 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000876 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100877 if (ret < 0) {
878 kfree(pl);
879 return ERR_PTR(ret);
880 }
881 }
882
Russell King24cf0e62019-12-11 10:56:35 +0000883 pl->cur_link_an_mode = pl->cfg_link_an_mode;
884
Russell King8fa7b9b2017-12-01 10:25:09 +0000885 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100886 if (ret < 0) {
887 kfree(pl);
888 return ERR_PTR(ret);
889 }
890
Russell King9525ae82017-07-25 15:03:13 +0100891 return pl;
892}
893EXPORT_SYMBOL_GPL(phylink_create);
894
Russell King7137e182020-07-21 12:04:46 +0100895/**
896 * phylink_set_pcs() - set the current PCS for phylink to use
897 * @pl: a pointer to a &struct phylink returned from phylink_create()
898 * @pcs: a pointer to the &struct phylink_pcs
899 *
900 * Bind the MAC PCS to phylink. This may be called after phylink_create(),
901 * in mac_prepare() or mac_config() methods if it is desired to dynamically
902 * change the PCS.
903 *
904 * Please note that there are behavioural changes with the mac_config()
905 * callback if a PCS is present (denoting a newer setup) so removing a PCS
906 * is not supported, and if a PCS is going to be used, it must be registered
907 * by calling phylink_set_pcs() at the latest in the first mac_config() call.
908 */
909void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs)
Russell King4c0d6d32020-03-30 18:44:55 +0100910{
Russell King7137e182020-07-21 12:04:46 +0100911 pl->pcs = pcs;
912 pl->pcs_ops = pcs->ops;
Russell King4c0d6d32020-03-30 18:44:55 +0100913}
Russell King7137e182020-07-21 12:04:46 +0100914EXPORT_SYMBOL_GPL(phylink_set_pcs);
Russell King4c0d6d32020-03-30 18:44:55 +0100915
Russell King8796c892017-12-01 10:24:47 +0000916/**
917 * phylink_destroy() - cleanup and destroy the phylink instance
918 * @pl: a pointer to a &struct phylink returned from phylink_create()
919 *
920 * Destroy a phylink instance. Any PHY that has been attached must have been
921 * cleaned up via phylink_disconnect_phy() prior to calling this function.
Russell King269a6b52019-11-19 17:18:52 +0000922 *
923 * Note: the rtnl lock must not be held when calling this function.
Russell King8796c892017-12-01 10:24:47 +0000924 */
Russell King9525ae82017-07-25 15:03:13 +0100925void phylink_destroy(struct phylink *pl)
926{
Russell King727b3662019-11-08 17:39:29 +0000927 sfp_bus_del_upstream(pl->sfp_bus);
Russell King7b3b0e82019-05-28 10:57:23 +0100928 if (pl->link_gpio)
Florian Fainellidaab3342018-05-10 13:17:30 -0700929 gpiod_put(pl->link_gpio);
Russell Kingce0aa272017-07-25 15:03:18 +0100930
Russell King9525ae82017-07-25 15:03:13 +0100931 cancel_work_sync(&pl->resolve);
932 kfree(pl);
933}
934EXPORT_SYMBOL_GPL(phylink_destroy);
935
Doug Bergera3075932020-05-18 15:23:59 -0700936static void phylink_phy_change(struct phy_device *phydev, bool up)
Russell King9525ae82017-07-25 15:03:13 +0100937{
938 struct phylink *pl = phydev->phylink;
Russell King33faac82020-02-15 15:49:48 +0000939 bool tx_pause, rx_pause;
940
941 phy_get_pause(phydev, &tx_pause, &rx_pause);
Russell King9525ae82017-07-25 15:03:13 +0100942
943 mutex_lock(&pl->state_mutex);
944 pl->phy_state.speed = phydev->speed;
945 pl->phy_state.duplex = phydev->duplex;
946 pl->phy_state.pause = MLO_PAUSE_NONE;
Russell King33faac82020-02-15 15:49:48 +0000947 if (tx_pause)
948 pl->phy_state.pause |= MLO_PAUSE_TX;
949 if (rx_pause)
950 pl->phy_state.pause |= MLO_PAUSE_RX;
Russell King9525ae82017-07-25 15:03:13 +0100951 pl->phy_state.interface = phydev->interface;
952 pl->phy_state.link = up;
953 mutex_unlock(&pl->state_mutex);
954
955 phylink_run_resolve(pl);
956
Russell King (Oracle)d34869b2021-07-20 12:15:20 +0100957 phylink_dbg(pl, "phy link %s %s/%s/%s/%s\n", up ? "up" : "down",
Ioana Ciornei170911802019-05-28 20:38:14 +0300958 phy_modes(phydev->interface),
959 phy_speed_to_str(phydev->speed),
Russell King (Oracle)d34869b2021-07-20 12:15:20 +0100960 phy_duplex_to_str(phydev->duplex),
961 phylink_pause_to_str(pl->phy_state.pause));
Russell King9525ae82017-07-25 15:03:13 +0100962}
963
Russell Kinge45d1f52019-12-11 10:56:30 +0000964static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
965 phy_interface_t interface)
Russell King9525ae82017-07-25 15:03:13 +0100966{
967 struct phylink_link_state config;
968 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
Florian Fainellie27f1782020-01-12 09:35:38 -0800969 char *irq_str;
Russell King9525ae82017-07-25 15:03:13 +0100970 int ret;
971
Russell King9525ae82017-07-25 15:03:13 +0100972 /*
973 * This is the new way of dealing with flow control for PHYs,
974 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
975 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
976 * using our validate call to the MAC, we rely upon the MAC
977 * clearing the bits from both supported and advertising fields.
978 */
Russell King725ea4b2019-11-15 20:05:45 +0000979 phy_support_asym_pause(phy);
980
981 memset(&config, 0, sizeof(config));
982 linkmode_copy(supported, phy->supported);
983 linkmode_copy(config.advertising, phy->advertising);
Russell Kingdf3f57a2019-12-13 18:22:07 +0000984
985 /* Clause 45 PHYs switch their Serdes lane between several different
986 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
987 * speeds. We really need to know which interface modes the PHY and
988 * MAC supports to properly work out which linkmodes can be supported.
989 */
990 if (phy->is_c45 &&
991 interface != PHY_INTERFACE_MODE_RXAUI &&
992 interface != PHY_INTERFACE_MODE_XAUI &&
993 interface != PHY_INTERFACE_MODE_USXGMII)
994 config.interface = PHY_INTERFACE_MODE_NA;
995 else
996 config.interface = interface;
Russell King9525ae82017-07-25 15:03:13 +0100997
998 ret = phylink_validate(pl, supported, &config);
Hauke Mehrtens20d8bb02020-03-02 00:55:02 +0100999 if (ret) {
1000 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
1001 phy_modes(config.interface),
1002 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1003 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1004 ret);
Russell King9525ae82017-07-25 15:03:13 +01001005 return ret;
Hauke Mehrtens20d8bb02020-03-02 00:55:02 +01001006 }
Russell King9525ae82017-07-25 15:03:13 +01001007
1008 phy->phylink = pl;
1009 phy->phy_link_change = phylink_phy_change;
1010
Florian Fainellie27f1782020-01-12 09:35:38 -08001011 irq_str = phy_attached_info_irq(phy);
Ioana Ciornei170911802019-05-28 20:38:14 +03001012 phylink_info(pl,
Florian Fainellie27f1782020-01-12 09:35:38 -08001013 "PHY [%s] driver [%s] (irq=%s)\n",
1014 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1015 kfree(irq_str);
Russell King9525ae82017-07-25 15:03:13 +01001016
1017 mutex_lock(&phy->lock);
1018 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +01001019 pl->phydev = phy;
Russell Kinge45d1f52019-12-11 10:56:30 +00001020 pl->phy_state.interface = interface;
Russell King97fec512020-02-15 15:50:03 +00001021 pl->phy_state.pause = MLO_PAUSE_NONE;
1022 pl->phy_state.speed = SPEED_UNKNOWN;
1023 pl->phy_state.duplex = DUPLEX_UNKNOWN;
Russell King9525ae82017-07-25 15:03:13 +01001024 linkmode_copy(pl->supported, supported);
1025 linkmode_copy(pl->link_config.advertising, config.advertising);
1026
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001027 /* Restrict the phy advertisement according to the MAC support. */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001028 linkmode_copy(phy->advertising, config.advertising);
Russell King9525ae82017-07-25 15:03:13 +01001029 mutex_unlock(&pl->state_mutex);
1030 mutex_unlock(&phy->lock);
1031
Ioana Ciornei170911802019-05-28 20:38:14 +03001032 phylink_dbg(pl,
1033 "phy: setting supported %*pb advertising %*pb\n",
1034 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1035 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +01001036
Heiner Kallweit434a4312019-01-23 07:31:58 +01001037 if (phy_interrupt_is_valid(phy))
1038 phy_request_interrupt(phy);
Russell King9525ae82017-07-25 15:03:13 +01001039
1040 return 0;
1041}
1042
Russell King938d44c2019-12-11 10:56:25 +00001043static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1044 phy_interface_t interface)
Baruch Siach7e418372018-10-03 19:04:49 +03001045{
Russell King24cf0e62019-12-11 10:56:35 +00001046 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1047 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
Baruch Siach7e418372018-10-03 19:04:49 +03001048 phy_interface_mode_is_8023z(interface))))
1049 return -EINVAL;
1050
1051 if (pl->phydev)
1052 return -EBUSY;
1053
Russell King938d44c2019-12-11 10:56:25 +00001054 return phy_attach_direct(pl->netdev, phy, 0, interface);
Baruch Siach7e418372018-10-03 19:04:49 +03001055}
1056
Russell King8796c892017-12-01 10:24:47 +00001057/**
1058 * phylink_connect_phy() - connect a PHY to the phylink instance
1059 * @pl: a pointer to a &struct phylink returned from phylink_create()
1060 * @phy: a pointer to a &struct phy_device.
1061 *
1062 * Connect @phy to the phylink instance specified by @pl by calling
1063 * phy_attach_direct(). Configure the @phy according to the MAC driver's
1064 * capabilities, start the PHYLIB state machine and enable any interrupts
1065 * that the PHY supports.
1066 *
1067 * This updates the phylink's ethtool supported and advertising link mode
1068 * masks.
1069 *
1070 * Returns 0 on success or a negative errno.
1071 */
Russell King9525ae82017-07-25 15:03:13 +01001072int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1073{
Russell King938d44c2019-12-11 10:56:25 +00001074 int ret;
1075
Florian Fainelli4904b6e2017-12-12 16:00:26 -08001076 /* Use PHY device/driver interface */
1077 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1078 pl->link_interface = phy->interface;
1079 pl->link_config.interface = pl->link_interface;
1080 }
1081
Russell King938d44c2019-12-11 10:56:25 +00001082 ret = phylink_attach_phy(pl, phy, pl->link_interface);
1083 if (ret < 0)
1084 return ret;
1085
Russell Kinge45d1f52019-12-11 10:56:30 +00001086 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
Russell King938d44c2019-12-11 10:56:25 +00001087 if (ret)
1088 phy_detach(phy);
1089
1090 return ret;
Russell King9525ae82017-07-25 15:03:13 +01001091}
1092EXPORT_SYMBOL_GPL(phylink_connect_phy);
1093
Russell King8796c892017-12-01 10:24:47 +00001094/**
1095 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1096 * @pl: a pointer to a &struct phylink returned from phylink_create()
1097 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -08001098 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +00001099 *
1100 * Connect the phy specified in the device node @dn to the phylink instance
1101 * specified by @pl. Actions specified in phylink_connect_phy() will be
1102 * performed.
1103 *
1104 * Returns 0 on success or a negative errno.
1105 */
Florian Fainelli0a629642017-12-12 16:00:25 -08001106int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1107 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +01001108{
Calvin Johnson423e6e82021-06-11 13:54:00 +03001109 return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
Russell King9525ae82017-07-25 15:03:13 +01001110}
1111EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1112
Russell King8796c892017-12-01 10:24:47 +00001113/**
Calvin Johnson25396f62021-06-11 13:53:59 +03001114 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
1115 * @pl: a pointer to a &struct phylink returned from phylink_create()
1116 * @fwnode: a pointer to a &struct fwnode_handle.
1117 * @flags: PHY-specific flags to communicate to the PHY device driver
1118 *
1119 * Connect the phy specified @fwnode to the phylink instance specified
1120 * by @pl.
1121 *
1122 * Returns 0 on success or a negative errno.
1123 */
1124int phylink_fwnode_phy_connect(struct phylink *pl,
1125 struct fwnode_handle *fwnode,
1126 u32 flags)
1127{
1128 struct fwnode_handle *phy_fwnode;
1129 struct phy_device *phy_dev;
1130 int ret;
1131
1132 /* Fixed links and 802.3z are handled without needing a PHY */
1133 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1134 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1135 phy_interface_mode_is_8023z(pl->link_interface)))
1136 return 0;
1137
1138 phy_fwnode = fwnode_get_phy_node(fwnode);
1139 if (IS_ERR(phy_fwnode)) {
1140 if (pl->cfg_link_an_mode == MLO_AN_PHY)
1141 return -ENODEV;
1142 return 0;
1143 }
1144
1145 phy_dev = fwnode_phy_find_device(phy_fwnode);
1146 /* We're done with the phy_node handle */
1147 fwnode_handle_put(phy_fwnode);
1148 if (!phy_dev)
1149 return -ENODEV;
1150
1151 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1152 pl->link_interface);
1153 if (ret) {
1154 phy_device_free(phy_dev);
1155 return ret;
1156 }
1157
1158 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1159 if (ret)
1160 phy_detach(phy_dev);
1161
1162 return ret;
1163}
1164EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
1165
1166/**
Russell King8796c892017-12-01 10:24:47 +00001167 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1168 * instance.
1169 * @pl: a pointer to a &struct phylink returned from phylink_create()
1170 *
1171 * Disconnect any current PHY from the phylink instance described by @pl.
1172 */
Russell King9525ae82017-07-25 15:03:13 +01001173void phylink_disconnect_phy(struct phylink *pl)
1174{
1175 struct phy_device *phy;
1176
Russell King8b874512017-12-15 16:09:47 +00001177 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001178
1179 phy = pl->phydev;
1180 if (phy) {
1181 mutex_lock(&phy->lock);
1182 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +01001183 pl->phydev = NULL;
1184 mutex_unlock(&pl->state_mutex);
1185 mutex_unlock(&phy->lock);
1186 flush_work(&pl->resolve);
1187
1188 phy_disconnect(phy);
1189 }
1190}
1191EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1192
Russell King8796c892017-12-01 10:24:47 +00001193/**
1194 * phylink_mac_change() - notify phylink of a change in MAC state
1195 * @pl: a pointer to a &struct phylink returned from phylink_create()
1196 * @up: indicates whether the link is currently up.
1197 *
1198 * The MAC driver should call this driver when the state of its link
1199 * changes (eg, link failure, new negotiation results, etc.)
1200 */
Russell King9525ae82017-07-25 15:03:13 +01001201void phylink_mac_change(struct phylink *pl, bool up)
1202{
1203 if (!up)
1204 pl->mac_link_dropped = true;
1205 phylink_run_resolve(pl);
Ioana Ciornei170911802019-05-28 20:38:14 +03001206 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
Russell King9525ae82017-07-25 15:03:13 +01001207}
1208EXPORT_SYMBOL_GPL(phylink_mac_change);
1209
Russell King7b3b0e82019-05-28 10:57:23 +01001210static irqreturn_t phylink_link_handler(int irq, void *data)
1211{
1212 struct phylink *pl = data;
1213
1214 phylink_run_resolve(pl);
1215
1216 return IRQ_HANDLED;
1217}
1218
Russell King8796c892017-12-01 10:24:47 +00001219/**
1220 * phylink_start() - start a phylink instance
1221 * @pl: a pointer to a &struct phylink returned from phylink_create()
1222 *
1223 * Start the phylink instance specified by @pl, configuring the MAC for the
1224 * desired link mode(s) and negotiation style. This should be called from the
1225 * network device driver's &struct net_device_ops ndo_open() method.
1226 */
Russell King9525ae82017-07-25 15:03:13 +01001227void phylink_start(struct phylink *pl)
1228{
Russell King5c05c1d2020-04-23 17:02:56 +01001229 bool poll = false;
1230
Russell King8b874512017-12-15 16:09:47 +00001231 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001232
Ioana Ciornei170911802019-05-28 20:38:14 +03001233 phylink_info(pl, "configuring for %s/%s link mode\n",
Russell King24cf0e62019-12-11 10:56:35 +00001234 phylink_an_mode_str(pl->cur_link_an_mode),
Ioana Ciornei170911802019-05-28 20:38:14 +03001235 phy_modes(pl->link_config.interface));
Russell King9525ae82017-07-25 15:03:13 +01001236
Antoine Tenartaeeb2e82018-09-19 11:39:31 +02001237 /* Always set the carrier off */
Ioana Ciornei43de6192019-05-28 20:38:13 +03001238 if (pl->netdev)
1239 netif_carrier_off(pl->netdev);
Antoine Tenartaeeb2e82018-09-19 11:39:31 +02001240
Russell King9525ae82017-07-25 15:03:13 +01001241 /* Apply the link configuration to the MAC when starting. This allows
1242 * a fixed-link to start with the correct parameters, and also
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001243 * ensures that we set the appropriate advertisement for Serdes links.
Russell King4c0d6d32020-03-30 18:44:55 +01001244 *
1245 * Restart autonegotiation if using 802.3z to ensure that the link
Russell King85b43942017-12-01 10:24:42 +00001246 * parameters are properly negotiated. This is necessary for DSA
1247 * switches using 802.3z negotiation to ensure they see our modes.
1248 */
Russell King4c0d6d32020-03-30 18:44:55 +01001249 phylink_mac_initial_config(pl, true);
Russell King85b43942017-12-01 10:24:42 +00001250
Russell King9525ae82017-07-25 15:03:13 +01001251 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1252 phylink_run_resolve(pl);
1253
Russell King24cf0e62019-12-11 10:56:35 +00001254 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
Russell King7b3b0e82019-05-28 10:57:23 +01001255 int irq = gpiod_to_irq(pl->link_gpio);
1256
1257 if (irq > 0) {
1258 if (!request_irq(irq, phylink_link_handler,
1259 IRQF_TRIGGER_RISING |
1260 IRQF_TRIGGER_FALLING,
1261 "netdev link", pl))
1262 pl->link_irq = irq;
1263 else
1264 irq = 0;
1265 }
1266 if (irq <= 0)
Russell King5c05c1d2020-04-23 17:02:56 +01001267 poll = true;
Russell King7b3b0e82019-05-28 10:57:23 +01001268 }
Russell King5c05c1d2020-04-23 17:02:56 +01001269
1270 switch (pl->cfg_link_an_mode) {
1271 case MLO_AN_FIXED:
1272 poll |= pl->config->poll_fixed_state;
1273 break;
1274 case MLO_AN_INBAND:
1275 poll |= pl->config->pcs_poll;
Russell King7137e182020-07-21 12:04:46 +01001276 if (pl->pcs)
1277 poll |= pl->pcs->poll;
Russell King5c05c1d2020-04-23 17:02:56 +01001278 break;
1279 }
1280 if (poll)
Russell King9cd00a82018-05-10 13:17:31 -07001281 mod_timer(&pl->link_poll, jiffies + HZ);
Russell King9525ae82017-07-25 15:03:13 +01001282 if (pl->phydev)
1283 phy_start(pl->phydev);
Arseny Solokhac7fa7f52019-07-24 20:31:39 +07001284 if (pl->sfp_bus)
1285 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +01001286}
1287EXPORT_SYMBOL_GPL(phylink_start);
1288
Russell King8796c892017-12-01 10:24:47 +00001289/**
1290 * phylink_stop() - stop a phylink instance
1291 * @pl: a pointer to a &struct phylink returned from phylink_create()
1292 *
1293 * Stop the phylink instance specified by @pl. This should be called from the
1294 * network device driver's &struct net_device_ops ndo_stop() method. The
1295 * network device's carrier state should not be changed prior to calling this
1296 * function.
Russell King (Oracle)f9749362021-09-07 18:56:46 +08001297 *
1298 * This will synchronously bring down the link if the link is not already
1299 * down (in other words, it will trigger a mac_link_down() method call.)
Russell King8796c892017-12-01 10:24:47 +00001300 */
Russell King9525ae82017-07-25 15:03:13 +01001301void phylink_stop(struct phylink *pl)
1302{
Russell King8b874512017-12-15 16:09:47 +00001303 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001304
Russell Kingce0aa272017-07-25 15:03:18 +01001305 if (pl->sfp_bus)
1306 sfp_upstream_stop(pl->sfp_bus);
Arseny Solokhac7fa7f52019-07-24 20:31:39 +07001307 if (pl->phydev)
1308 phy_stop(pl->phydev);
Russell King7b3b0e82019-05-28 10:57:23 +01001309 del_timer_sync(&pl->link_poll);
1310 if (pl->link_irq) {
1311 free_irq(pl->link_irq, pl);
1312 pl->link_irq = 0;
1313 }
Russell King9525ae82017-07-25 15:03:13 +01001314
Russell King87454b62019-02-11 15:04:24 +00001315 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
Russell King9525ae82017-07-25 15:03:13 +01001316}
1317EXPORT_SYMBOL_GPL(phylink_stop);
1318
Russell King8796c892017-12-01 10:24:47 +00001319/**
Russell King (Oracle)f9749362021-09-07 18:56:46 +08001320 * phylink_suspend() - handle a network device suspend event
1321 * @pl: a pointer to a &struct phylink returned from phylink_create()
1322 * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
1323 *
1324 * Handle a network device suspend event. There are several cases:
1325 * - If Wake-on-Lan is not active, we can bring down the link between
1326 * the MAC and PHY by calling phylink_stop().
1327 * - If Wake-on-Lan is active, and being handled only by the PHY, we
1328 * can also bring down the link between the MAC and PHY.
1329 * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
1330 * still needs to receive packets, so we can not bring the link down.
1331 */
1332void phylink_suspend(struct phylink *pl, bool mac_wol)
1333{
1334 ASSERT_RTNL();
1335
1336 if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) {
1337 /* Wake-on-Lan enabled, MAC handling */
1338 mutex_lock(&pl->state_mutex);
1339
1340 /* Stop the resolver bringing the link up */
1341 __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1342
1343 /* Disable the carrier, to prevent transmit timeouts,
1344 * but one would hope all packets have been sent. This
1345 * also means phylink_resolve() will do nothing.
1346 */
Russell King (Oracle)cbcca2e2021-09-17 14:36:31 +01001347 if (pl->netdev)
1348 netif_carrier_off(pl->netdev);
1349 else
1350 pl->old_link_state = false;
Russell King (Oracle)f9749362021-09-07 18:56:46 +08001351
1352 /* We do not call mac_link_down() here as we want the
1353 * link to remain up to receive the WoL packets.
1354 */
1355 mutex_unlock(&pl->state_mutex);
1356 } else {
1357 phylink_stop(pl);
1358 }
1359}
1360EXPORT_SYMBOL_GPL(phylink_suspend);
1361
1362/**
1363 * phylink_resume() - handle a network device resume event
1364 * @pl: a pointer to a &struct phylink returned from phylink_create()
1365 *
1366 * Undo the effects of phylink_suspend(), returning the link to an
1367 * operational state.
1368 */
1369void phylink_resume(struct phylink *pl)
1370{
1371 ASSERT_RTNL();
1372
1373 if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
1374 /* Wake-on-Lan enabled, MAC handling */
1375
1376 /* Call mac_link_down() so we keep the overall state balanced.
1377 * Do this under the state_mutex lock for consistency. This
1378 * will cause a "Link Down" message to be printed during
1379 * resume, which is harmless - the true link state will be
1380 * printed when we run a resolve.
1381 */
1382 mutex_lock(&pl->state_mutex);
1383 phylink_link_down(pl);
1384 mutex_unlock(&pl->state_mutex);
1385
1386 /* Re-apply the link parameters so that all the settings get
1387 * restored to the MAC.
1388 */
1389 phylink_mac_initial_config(pl, true);
1390
1391 /* Re-enable and re-resolve the link parameters */
1392 clear_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1393 phylink_run_resolve(pl);
1394 } else {
1395 phylink_start(pl);
1396 }
1397}
1398EXPORT_SYMBOL_GPL(phylink_resume);
1399
1400/**
Russell King8796c892017-12-01 10:24:47 +00001401 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1402 * @pl: a pointer to a &struct phylink returned from phylink_create()
1403 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1404 *
1405 * Read the wake on lan parameters from the PHY attached to the phylink
1406 * instance specified by @pl. If no PHY is currently attached, report no
1407 * support for wake on lan.
1408 */
Russell King9525ae82017-07-25 15:03:13 +01001409void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1410{
Russell King8b874512017-12-15 16:09:47 +00001411 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001412
1413 wol->supported = 0;
1414 wol->wolopts = 0;
1415
1416 if (pl->phydev)
1417 phy_ethtool_get_wol(pl->phydev, wol);
1418}
1419EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1420
Russell King8796c892017-12-01 10:24:47 +00001421/**
1422 * phylink_ethtool_set_wol() - set wake on lan parameters
1423 * @pl: a pointer to a &struct phylink returned from phylink_create()
1424 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1425 *
1426 * Set the wake on lan parameters for the PHY attached to the phylink
1427 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1428 * error.
1429 *
1430 * Returns zero on success or negative errno code.
1431 */
Russell King9525ae82017-07-25 15:03:13 +01001432int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1433{
1434 int ret = -EOPNOTSUPP;
1435
Russell King8b874512017-12-15 16:09:47 +00001436 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001437
1438 if (pl->phydev)
1439 ret = phy_ethtool_set_wol(pl->phydev, wol);
1440
1441 return ret;
1442}
1443EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1444
1445static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1446{
1447 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1448
1449 linkmode_zero(mask);
1450 phylink_set_port_modes(mask);
1451
1452 linkmode_and(dst, dst, mask);
1453 linkmode_or(dst, dst, b);
1454}
1455
1456static void phylink_get_ksettings(const struct phylink_link_state *state,
1457 struct ethtool_link_ksettings *kset)
1458{
1459 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1460 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1461 kset->base.speed = state->speed;
1462 kset->base.duplex = state->duplex;
1463 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1464 AUTONEG_DISABLE;
1465}
1466
Russell King8796c892017-12-01 10:24:47 +00001467/**
1468 * phylink_ethtool_ksettings_get() - get the current link settings
1469 * @pl: a pointer to a &struct phylink returned from phylink_create()
1470 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1471 *
1472 * Read the current link settings for the phylink instance specified by @pl.
1473 * This will be the link settings read from the MAC, PHY or fixed link
1474 * settings depending on the current negotiation mode.
1475 */
Russell King9525ae82017-07-25 15:03:13 +01001476int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001477 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001478{
1479 struct phylink_link_state link_state;
1480
Russell King8b874512017-12-15 16:09:47 +00001481 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001482
Wenpeng Liang169d7a42021-06-16 18:01:23 +08001483 if (pl->phydev)
Russell King9525ae82017-07-25 15:03:13 +01001484 phy_ethtool_ksettings_get(pl->phydev, kset);
Wenpeng Liang169d7a42021-06-16 18:01:23 +08001485 else
Russell King9525ae82017-07-25 15:03:13 +01001486 kset->base.port = pl->link_port;
Russell King9525ae82017-07-25 15:03:13 +01001487
1488 linkmode_copy(kset->link_modes.supported, pl->supported);
1489
Russell King24cf0e62019-12-11 10:56:35 +00001490 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +01001491 case MLO_AN_FIXED:
1492 /* We are using fixed settings. Report these as the
1493 * current link settings - and note that these also
1494 * represent the supported speeds/duplex/pause modes.
1495 */
1496 phylink_get_fixed_state(pl, &link_state);
1497 phylink_get_ksettings(&link_state, kset);
1498 break;
1499
Russell King86a362c2017-12-01 10:24:26 +00001500 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001501 /* If there is a phy attached, then use the reported
1502 * settings from the phy with no modification.
1503 */
1504 if (pl->phydev)
1505 break;
1506
Russell Kingd46b7e42019-11-21 00:36:22 +00001507 phylink_mac_pcs_get_state(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +01001508
1509 /* The MAC is reporting the link results from its own PCS
1510 * layer via in-band status. Report these as the current
1511 * link settings.
1512 */
1513 phylink_get_ksettings(&link_state, kset);
1514 break;
1515 }
1516
1517 return 0;
1518}
1519EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1520
Russell King8796c892017-12-01 10:24:47 +00001521/**
1522 * phylink_ethtool_ksettings_set() - set the link settings
1523 * @pl: a pointer to a &struct phylink returned from phylink_create()
1524 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1525 */
Russell King9525ae82017-07-25 15:03:13 +01001526int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001527 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001528{
Russell King77316762019-06-02 15:12:54 +01001529 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
Russell King9525ae82017-07-25 15:03:13 +01001530 struct phylink_link_state config;
Russell Kingc8cab712020-07-21 12:04:16 +01001531 const struct phy_setting *s;
Russell King9525ae82017-07-25 15:03:13 +01001532
Russell King8b874512017-12-15 16:09:47 +00001533 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001534
Russell Kingcbc1bb12020-07-21 12:04:21 +01001535 if (pl->phydev) {
1536 /* We can rely on phylib for this update; we also do not need
1537 * to update the pl->link_config settings:
1538 * - the configuration returned via ksettings_get() will come
1539 * from phylib whenever a PHY is present.
1540 * - link_config.interface will be updated by the PHY calling
1541 * back via phylink_phy_change() and a subsequent resolve.
1542 * - initial link configuration for PHY mode comes from the
1543 * last phy state updated via phylink_phy_change().
1544 * - other configuration changes (e.g. pause modes) are
1545 * performed directly via phylib.
1546 * - if in in-band mode with a PHY, the link configuration
1547 * is passed on the link from the PHY, and all of
1548 * link_config.{speed,duplex,an_enabled,pause} are not used.
1549 * - the only possible use would be link_config.advertising
1550 * pause modes when in 1000base-X mode with a PHY, but in
1551 * the presence of a PHY, this should not be changed as that
1552 * should be determined from the media side advertisement.
1553 */
1554 return phy_ethtool_ksettings_set(pl->phydev, kset);
1555 }
1556
Russell King9525ae82017-07-25 15:03:13 +01001557 config = pl->link_config;
1558
Russell King (Oracle)7cefb0b2021-07-20 12:15:26 +01001559 /* Mask out unsupported advertisements */
Russell King9525ae82017-07-25 15:03:13 +01001560 linkmode_and(config.advertising, kset->link_modes.advertising,
Russell King (Oracle)7cefb0b2021-07-20 12:15:26 +01001561 pl->supported);
Russell King9525ae82017-07-25 15:03:13 +01001562
1563 /* FIXME: should we reject autoneg if phy/mac does not support it? */
Russell Kingc8cab712020-07-21 12:04:16 +01001564 switch (kset->base.autoneg) {
1565 case AUTONEG_DISABLE:
Russell King9525ae82017-07-25 15:03:13 +01001566 /* Autonegotiation disabled, select a suitable speed and
1567 * duplex.
1568 */
1569 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
Russell King (Oracle)7cefb0b2021-07-20 12:15:26 +01001570 pl->supported, false);
Russell King9525ae82017-07-25 15:03:13 +01001571 if (!s)
1572 return -EINVAL;
1573
Russell King1e1bf142020-07-21 12:04:31 +01001574 /* If we have a fixed link, refuse to change link parameters.
1575 * If the link parameters match, accept them but do nothing.
Russell King9525ae82017-07-25 15:03:13 +01001576 */
Russell King1e1bf142020-07-21 12:04:31 +01001577 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1578 if (s->speed != pl->link_config.speed ||
1579 s->duplex != pl->link_config.duplex)
1580 return -EINVAL;
1581 return 0;
1582 }
Russell King9525ae82017-07-25 15:03:13 +01001583
1584 config.speed = s->speed;
1585 config.duplex = s->duplex;
Russell Kingc8cab712020-07-21 12:04:16 +01001586 break;
Russell King9525ae82017-07-25 15:03:13 +01001587
Russell Kingc8cab712020-07-21 12:04:16 +01001588 case AUTONEG_ENABLE:
Russell King1e1bf142020-07-21 12:04:31 +01001589 /* If we have a fixed link, allow autonegotiation (since that
1590 * is our default case) but do not allow the advertisement to
1591 * be changed. If the advertisement matches, simply return.
1592 */
1593 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1594 if (!linkmode_equal(config.advertising,
1595 pl->link_config.advertising))
1596 return -EINVAL;
1597 return 0;
1598 }
Russell King9525ae82017-07-25 15:03:13 +01001599
1600 config.speed = SPEED_UNKNOWN;
1601 config.duplex = DUPLEX_UNKNOWN;
Russell Kingc8cab712020-07-21 12:04:16 +01001602 break;
Russell King9525ae82017-07-25 15:03:13 +01001603
Russell Kingc8cab712020-07-21 12:04:16 +01001604 default:
1605 return -EINVAL;
Russell King9525ae82017-07-25 15:03:13 +01001606 }
1607
Russell King1e1bf142020-07-21 12:04:31 +01001608 /* We have ruled out the case with a PHY attached, and the
1609 * fixed-link cases. All that is left are in-band links.
Russell Kingcbc1bb12020-07-21 12:04:21 +01001610 */
Russell King (Oracle)7cefb0b2021-07-20 12:15:26 +01001611 config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
1612 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
1613 config.an_enabled);
1614
Nathan Rossiea269a62021-09-02 05:14:49 +00001615 /* If this link is with an SFP, ensure that changes to advertised modes
1616 * also cause the associated interface to be selected such that the
1617 * link can be configured correctly.
1618 */
Russell King (Oracle)dc906042021-10-19 11:00:04 +01001619 if (pl->sfp_bus) {
Nathan Rossiea269a62021-09-02 05:14:49 +00001620 config.interface = sfp_select_interface(pl->sfp_bus,
1621 config.advertising);
1622 if (config.interface == PHY_INTERFACE_MODE_NA) {
1623 phylink_err(pl,
1624 "selection of interface failed, advertisement %*pb\n",
1625 __ETHTOOL_LINK_MODE_MASK_NBITS,
1626 config.advertising);
1627 return -EINVAL;
1628 }
1629
1630 /* Revalidate with the selected interface */
1631 linkmode_copy(support, pl->supported);
1632 if (phylink_validate(pl, support, &config)) {
1633 phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
1634 phylink_an_mode_str(pl->cur_link_an_mode),
1635 phy_modes(config.interface),
1636 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1637 return -EINVAL;
1638 }
Russell King (Oracle)dc906042021-10-19 11:00:04 +01001639 } else {
1640 /* Validate without changing the current supported mask. */
1641 linkmode_copy(support, pl->supported);
1642 if (phylink_validate(pl, support, &config))
1643 return -EINVAL;
Nathan Rossiea269a62021-09-02 05:14:49 +00001644 }
1645
Russell King (Oracle)dc906042021-10-19 11:00:04 +01001646 /* If autonegotiation is enabled, we must have an advertisement */
1647 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1648 return -EINVAL;
1649
Russell Kingcbc1bb12020-07-21 12:04:21 +01001650 mutex_lock(&pl->state_mutex);
Russell Kingcbc1bb12020-07-21 12:04:21 +01001651 pl->link_config.speed = config.speed;
1652 pl->link_config.duplex = config.duplex;
Russell Kinga83c8822020-07-21 12:04:26 +01001653 pl->link_config.an_enabled = config.an_enabled;
Russell Kingcbc1bb12020-07-21 12:04:21 +01001654
Russell Kingb7ad14c2020-07-21 12:04:41 +01001655 if (pl->link_config.interface != config.interface) {
1656 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
1657 /* We need to force the link down, then change the interface */
1658 if (pl->old_link_state) {
1659 phylink_link_down(pl);
1660 pl->old_link_state = false;
1661 }
1662 if (!test_bit(PHYLINK_DISABLE_STOPPED,
1663 &pl->phylink_disable_state))
1664 phylink_major_config(pl, false, &config);
1665 pl->link_config.interface = config.interface;
1666 linkmode_copy(pl->link_config.advertising, config.advertising);
1667 } else if (!linkmode_equal(pl->link_config.advertising,
1668 config.advertising)) {
1669 linkmode_copy(pl->link_config.advertising, config.advertising);
1670 phylink_change_inband_advert(pl);
Russell King9525ae82017-07-25 15:03:13 +01001671 }
Russell Kingcbc1bb12020-07-21 12:04:21 +01001672 mutex_unlock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +01001673
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001674 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001675}
1676EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1677
Russell King8796c892017-12-01 10:24:47 +00001678/**
1679 * phylink_ethtool_nway_reset() - restart negotiation
1680 * @pl: a pointer to a &struct phylink returned from phylink_create()
1681 *
1682 * Restart negotiation for the phylink instance specified by @pl. This will
1683 * cause any attached phy to restart negotiation with the link partner, and
1684 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1685 * negotiation.
1686 *
1687 * Returns zero on success, or negative error code.
1688 */
Russell King9525ae82017-07-25 15:03:13 +01001689int phylink_ethtool_nway_reset(struct phylink *pl)
1690{
1691 int ret = 0;
1692
Russell King8b874512017-12-15 16:09:47 +00001693 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001694
1695 if (pl->phydev)
1696 ret = phy_restart_aneg(pl->phydev);
Russell King4c0d6d32020-03-30 18:44:55 +01001697 phylink_mac_pcs_an_restart(pl);
Russell King9525ae82017-07-25 15:03:13 +01001698
1699 return ret;
1700}
1701EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1702
Russell King8796c892017-12-01 10:24:47 +00001703/**
1704 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1705 * @pl: a pointer to a &struct phylink returned from phylink_create()
1706 * @pause: a pointer to a &struct ethtool_pauseparam
1707 */
Russell King9525ae82017-07-25 15:03:13 +01001708void phylink_ethtool_get_pauseparam(struct phylink *pl,
1709 struct ethtool_pauseparam *pause)
1710{
Russell King8b874512017-12-15 16:09:47 +00001711 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001712
1713 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1714 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1715 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1716}
1717EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1718
Russell King8796c892017-12-01 10:24:47 +00001719/**
1720 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1721 * @pl: a pointer to a &struct phylink returned from phylink_create()
1722 * @pause: a pointer to a &struct ethtool_pauseparam
1723 */
Russell King9525ae82017-07-25 15:03:13 +01001724int phylink_ethtool_set_pauseparam(struct phylink *pl,
1725 struct ethtool_pauseparam *pause)
1726{
1727 struct phylink_link_state *config = &pl->link_config;
Russell King2e919bc2020-06-23 17:47:29 +01001728 bool manual_changed;
1729 int pause_state;
Russell King9525ae82017-07-25 15:03:13 +01001730
Russell King8b874512017-12-15 16:09:47 +00001731 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001732
Russell King8cdfa252020-02-15 15:49:37 +00001733 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1734 return -EOPNOTSUPP;
1735
Russell King9525ae82017-07-25 15:03:13 +01001736 if (!phylink_test(pl->supported, Pause) &&
1737 !phylink_test(pl->supported, Asym_Pause))
1738 return -EOPNOTSUPP;
1739
1740 if (!phylink_test(pl->supported, Asym_Pause) &&
1741 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1742 return -EINVAL;
1743
Russell King2e919bc2020-06-23 17:47:29 +01001744 pause_state = 0;
Russell King9525ae82017-07-25 15:03:13 +01001745 if (pause->autoneg)
Russell King2e919bc2020-06-23 17:47:29 +01001746 pause_state |= MLO_PAUSE_AN;
Russell King9525ae82017-07-25 15:03:13 +01001747 if (pause->rx_pause)
Russell King2e919bc2020-06-23 17:47:29 +01001748 pause_state |= MLO_PAUSE_RX;
Russell King9525ae82017-07-25 15:03:13 +01001749 if (pause->tx_pause)
Russell King2e919bc2020-06-23 17:47:29 +01001750 pause_state |= MLO_PAUSE_TX;
Russell King9525ae82017-07-25 15:03:13 +01001751
Russell King2e919bc2020-06-23 17:47:29 +01001752 mutex_lock(&pl->state_mutex);
Russell Kingf904f152020-02-15 15:49:58 +00001753 /*
1754 * See the comments for linkmode_set_pause(), wrt the deficiencies
1755 * with the current implementation. A solution to this issue would
1756 * be:
1757 * ethtool Local device
1758 * rx tx Pause AsymDir
1759 * 0 0 0 0
1760 * 1 0 1 1
1761 * 0 1 0 1
1762 * 1 1 1 1
1763 * and then use the ethtool rx/tx enablement status to mask the
1764 * rx/tx pause resolution.
1765 */
1766 linkmode_set_pause(config->advertising, pause->tx_pause,
1767 pause->rx_pause);
1768
Russell King2e919bc2020-06-23 17:47:29 +01001769 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1770 (!(pause_state & MLO_PAUSE_AN) &&
1771 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1772
1773 config->pause = pause_state;
1774
Russell King1571e702020-07-21 12:04:36 +01001775 /* Update our in-band advertisement, triggering a renegotiation if
1776 * the advertisement changed.
1777 */
1778 if (!pl->phydev)
1779 phylink_change_inband_advert(pl);
Russell Kingc718af22020-06-23 17:47:23 +01001780
1781 mutex_unlock(&pl->state_mutex);
1782
1783 /* If we have a PHY, a change of the pause frame advertisement will
1784 * cause phylib to renegotiate (if AN is enabled) which will in turn
1785 * call our phylink_phy_change() and trigger a resolve. Note that
1786 * we can't hold our state mutex while calling phy_set_asym_pause().
Russell Kingd9922c02019-11-19 17:28:14 +00001787 */
Russell Kingc718af22020-06-23 17:47:23 +01001788 if (pl->phydev)
Russell Kingd9922c02019-11-19 17:28:14 +00001789 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1790 pause->tx_pause);
Russell King9525ae82017-07-25 15:03:13 +01001791
Russell King2e919bc2020-06-23 17:47:29 +01001792 /* If the manual pause settings changed, make sure we trigger a
1793 * resolve to update their state; we can not guarantee that the
1794 * link will cycle.
1795 */
1796 if (manual_changed) {
1797 pl->mac_link_dropped = true;
1798 phylink_run_resolve(pl);
Russell King9525ae82017-07-25 15:03:13 +01001799 }
Russell King9525ae82017-07-25 15:03:13 +01001800
1801 return 0;
1802}
1803EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1804
1805/**
Mauro Carvalho Chehab69280222020-11-16 11:17:57 +01001806 * phylink_get_eee_err() - read the energy efficient ethernet error
Russell King9525ae82017-07-25 15:03:13 +01001807 * counter
1808 * @pl: a pointer to a &struct phylink returned from phylink_create().
1809 *
1810 * Read the Energy Efficient Ethernet error counter from the PHY associated
1811 * with the phylink instance specified by @pl.
1812 *
1813 * Returns positive error counter value, or negative error code.
1814 */
1815int phylink_get_eee_err(struct phylink *pl)
1816{
1817 int ret = 0;
1818
Russell King8b874512017-12-15 16:09:47 +00001819 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001820
1821 if (pl->phydev)
1822 ret = phy_get_eee_err(pl->phydev);
1823
1824 return ret;
1825}
1826EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1827
Russell King8796c892017-12-01 10:24:47 +00001828/**
Russell King86e58132019-02-11 11:46:06 +00001829 * phylink_init_eee() - init and check the EEE features
1830 * @pl: a pointer to a &struct phylink returned from phylink_create()
1831 * @clk_stop_enable: allow PHY to stop receive clock
1832 *
1833 * Must be called either with RTNL held or within mac_link_up()
1834 */
1835int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1836{
1837 int ret = -EOPNOTSUPP;
1838
1839 if (pl->phydev)
1840 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1841
1842 return ret;
1843}
1844EXPORT_SYMBOL_GPL(phylink_init_eee);
1845
1846/**
Russell King8796c892017-12-01 10:24:47 +00001847 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1848 * @pl: a pointer to a &struct phylink returned from phylink_create()
1849 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1850 */
Russell King9525ae82017-07-25 15:03:13 +01001851int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1852{
1853 int ret = -EOPNOTSUPP;
1854
Russell King8b874512017-12-15 16:09:47 +00001855 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001856
1857 if (pl->phydev)
1858 ret = phy_ethtool_get_eee(pl->phydev, eee);
1859
1860 return ret;
1861}
1862EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1863
Russell King8796c892017-12-01 10:24:47 +00001864/**
1865 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1866 * @pl: a pointer to a &struct phylink returned from phylink_create()
1867 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1868 */
Russell King9525ae82017-07-25 15:03:13 +01001869int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1870{
1871 int ret = -EOPNOTSUPP;
1872
Russell King8b874512017-12-15 16:09:47 +00001873 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001874
1875 if (pl->phydev)
1876 ret = phy_ethtool_set_eee(pl->phydev, eee);
1877
1878 return ret;
1879}
1880EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1881
1882/* This emulates MII registers for a fixed-mode phy operating as per the
1883 * passed in state. "aneg" defines if we report negotiation is possible.
1884 *
1885 * FIXME: should deal with negotiation state too.
1886 */
Russell King7fdc4552019-05-28 10:57:18 +01001887static int phylink_mii_emul_read(unsigned int reg,
1888 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +01001889{
1890 struct fixed_phy_status fs;
Russell King4e5aeb42020-02-15 15:49:53 +00001891 unsigned long *lpa = state->lp_advertising;
Russell King9525ae82017-07-25 15:03:13 +01001892 int val;
1893
1894 fs.link = state->link;
1895 fs.speed = state->speed;
1896 fs.duplex = state->duplex;
Russell King4e5aeb42020-02-15 15:49:53 +00001897 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1898 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
Russell King9525ae82017-07-25 15:03:13 +01001899
1900 val = swphy_read_reg(reg, &fs);
1901 if (reg == MII_BMSR) {
1902 if (!state->an_complete)
1903 val &= ~BMSR_ANEGCOMPLETE;
Russell King9525ae82017-07-25 15:03:13 +01001904 }
1905 return val;
1906}
1907
Russell Kingecbd87b2017-07-25 15:03:28 +01001908static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1909 unsigned int reg)
1910{
1911 struct phy_device *phydev = pl->phydev;
1912 int prtad, devad;
1913
1914 if (mdio_phy_id_is_c45(phy_id)) {
1915 prtad = mdio_phy_id_prtad(phy_id);
1916 devad = mdio_phy_id_devad(phy_id);
Russell King90ce6652020-05-26 16:29:36 +01001917 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001918 } else if (phydev->is_c45) {
1919 switch (reg) {
1920 case MII_BMCR:
1921 case MII_BMSR:
1922 case MII_PHYSID1:
1923 case MII_PHYSID2:
Russell King320ed3b2020-06-18 14:46:08 +01001924 devad = __ffs(phydev->c45_ids.mmds_present);
Russell Kingecbd87b2017-07-25 15:03:28 +01001925 break;
1926 case MII_ADVERTISE:
1927 case MII_LPA:
Russell King320ed3b2020-06-18 14:46:08 +01001928 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
Russell Kingecbd87b2017-07-25 15:03:28 +01001929 return -EINVAL;
1930 devad = MDIO_MMD_AN;
1931 if (reg == MII_ADVERTISE)
1932 reg = MDIO_AN_ADVERTISE;
1933 else
1934 reg = MDIO_AN_LPA;
1935 break;
1936 default:
1937 return -EINVAL;
1938 }
1939 prtad = phy_id;
Russell King90ce6652020-05-26 16:29:36 +01001940 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001941 } else {
1942 prtad = phy_id;
1943 devad = reg;
1944 }
1945 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1946}
1947
1948static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1949 unsigned int reg, unsigned int val)
1950{
1951 struct phy_device *phydev = pl->phydev;
1952 int prtad, devad;
1953
1954 if (mdio_phy_id_is_c45(phy_id)) {
1955 prtad = mdio_phy_id_prtad(phy_id);
1956 devad = mdio_phy_id_devad(phy_id);
Russell King90ce6652020-05-26 16:29:36 +01001957 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001958 } else if (phydev->is_c45) {
1959 switch (reg) {
1960 case MII_BMCR:
1961 case MII_BMSR:
1962 case MII_PHYSID1:
1963 case MII_PHYSID2:
Russell King320ed3b2020-06-18 14:46:08 +01001964 devad = __ffs(phydev->c45_ids.mmds_present);
Russell Kingecbd87b2017-07-25 15:03:28 +01001965 break;
1966 case MII_ADVERTISE:
1967 case MII_LPA:
Russell King320ed3b2020-06-18 14:46:08 +01001968 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
Russell Kingecbd87b2017-07-25 15:03:28 +01001969 return -EINVAL;
1970 devad = MDIO_MMD_AN;
1971 if (reg == MII_ADVERTISE)
1972 reg = MDIO_AN_ADVERTISE;
1973 else
1974 reg = MDIO_AN_LPA;
1975 break;
1976 default:
1977 return -EINVAL;
1978 }
1979 prtad = phy_id;
Russell King90ce6652020-05-26 16:29:36 +01001980 devad = mdiobus_c45_addr(devad, reg);
Russell Kingecbd87b2017-07-25 15:03:28 +01001981 } else {
1982 prtad = phy_id;
1983 devad = reg;
1984 }
1985
1986 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1987}
1988
Russell King9525ae82017-07-25 15:03:13 +01001989static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1990 unsigned int reg)
1991{
1992 struct phylink_link_state state;
1993 int val = 0xffff;
1994
Russell King24cf0e62019-12-11 10:56:35 +00001995 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +01001996 case MLO_AN_FIXED:
1997 if (phy_id == 0) {
1998 phylink_get_fixed_state(pl, &state);
Russell King7fdc4552019-05-28 10:57:18 +01001999 val = phylink_mii_emul_read(reg, &state);
Russell King9525ae82017-07-25 15:03:13 +01002000 }
2001 break;
2002
2003 case MLO_AN_PHY:
2004 return -EOPNOTSUPP;
2005
Russell King86a362c2017-12-01 10:24:26 +00002006 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01002007 if (phy_id == 0) {
Russell Kingd46b7e42019-11-21 00:36:22 +00002008 phylink_mac_pcs_get_state(pl, &state);
Russell King7fdc4552019-05-28 10:57:18 +01002009 val = phylink_mii_emul_read(reg, &state);
Russell King9525ae82017-07-25 15:03:13 +01002010 }
2011 break;
2012 }
2013
2014 return val & 0xffff;
2015}
2016
2017static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
2018 unsigned int reg, unsigned int val)
2019{
Russell King24cf0e62019-12-11 10:56:35 +00002020 switch (pl->cur_link_an_mode) {
Russell King9525ae82017-07-25 15:03:13 +01002021 case MLO_AN_FIXED:
2022 break;
2023
2024 case MLO_AN_PHY:
2025 return -EOPNOTSUPP;
2026
Russell King86a362c2017-12-01 10:24:26 +00002027 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01002028 break;
2029 }
2030
2031 return 0;
2032}
2033
Russell King8796c892017-12-01 10:24:47 +00002034/**
2035 * phylink_mii_ioctl() - generic mii ioctl interface
2036 * @pl: a pointer to a &struct phylink returned from phylink_create()
2037 * @ifr: a pointer to a &struct ifreq for socket ioctls
2038 * @cmd: ioctl cmd to execute
2039 *
2040 * Perform the specified MII ioctl on the PHY attached to the phylink instance
2041 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
2042 *
2043 * Returns: zero on success or negative error code.
2044 *
2045 * %SIOCGMIIPHY:
2046 * read register from the current PHY.
2047 * %SIOCGMIIREG:
2048 * read register from the specified PHY.
2049 * %SIOCSMIIREG:
2050 * set a register on the specified PHY.
2051 */
Russell King9525ae82017-07-25 15:03:13 +01002052int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
2053{
Russell Kingecbd87b2017-07-25 15:03:28 +01002054 struct mii_ioctl_data *mii = if_mii(ifr);
2055 int ret;
Russell King9525ae82017-07-25 15:03:13 +01002056
Russell King8b874512017-12-15 16:09:47 +00002057 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01002058
Russell Kingecbd87b2017-07-25 15:03:28 +01002059 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00002060 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01002061 switch (cmd) {
2062 case SIOCGMIIPHY:
2063 mii->phy_id = pl->phydev->mdio.addr;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002064 fallthrough;
Russell King9525ae82017-07-25 15:03:13 +01002065
Russell Kingecbd87b2017-07-25 15:03:28 +01002066 case SIOCGMIIREG:
2067 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
2068 if (ret >= 0) {
2069 mii->val_out = ret;
2070 ret = 0;
2071 }
2072 break;
Russell King9525ae82017-07-25 15:03:13 +01002073
Russell Kingecbd87b2017-07-25 15:03:28 +01002074 case SIOCSMIIREG:
2075 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
2076 mii->val_in);
2077 break;
Russell King9525ae82017-07-25 15:03:13 +01002078
Russell Kingecbd87b2017-07-25 15:03:28 +01002079 default:
Russell King9525ae82017-07-25 15:03:13 +01002080 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01002081 break;
2082 }
2083 } else {
2084 switch (cmd) {
2085 case SIOCGMIIPHY:
2086 mii->phy_id = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002087 fallthrough;
Russell Kingecbd87b2017-07-25 15:03:28 +01002088
2089 case SIOCGMIIREG:
2090 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
2091 if (ret >= 0) {
2092 mii->val_out = ret;
2093 ret = 0;
2094 }
2095 break;
2096
2097 case SIOCSMIIREG:
2098 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
2099 mii->val_in);
2100 break;
2101
2102 default:
2103 ret = -EOPNOTSUPP;
2104 break;
2105 }
Russell King9525ae82017-07-25 15:03:13 +01002106 }
2107
2108 return ret;
2109}
2110EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2111
Russell Kingc6d5d842020-06-24 11:06:54 +01002112/**
2113 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2114 * link partners
2115 * @pl: a pointer to a &struct phylink returned from phylink_create()
2116 * @sync: perform action synchronously
2117 *
2118 * If we have a PHY that is not part of a SFP module, then set the speed
2119 * as described in the phy_speed_down() function. Please see this function
2120 * for a description of the @sync parameter.
2121 *
2122 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2123 */
2124int phylink_speed_down(struct phylink *pl, bool sync)
2125{
2126 int ret = 0;
2127
2128 ASSERT_RTNL();
2129
2130 if (!pl->sfp_bus && pl->phydev)
2131 ret = phy_speed_down(pl->phydev, sync);
2132
2133 return ret;
2134}
2135EXPORT_SYMBOL_GPL(phylink_speed_down);
2136
2137/**
2138 * phylink_speed_up() - restore the advertised speeds prior to the call to
2139 * phylink_speed_down()
2140 * @pl: a pointer to a &struct phylink returned from phylink_create()
2141 *
2142 * If we have a PHY that is not part of a SFP module, then restore the
2143 * PHY speeds as per phy_speed_up().
2144 *
2145 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2146 */
2147int phylink_speed_up(struct phylink *pl)
2148{
2149 int ret = 0;
2150
2151 ASSERT_RTNL();
2152
2153 if (!pl->sfp_bus && pl->phydev)
2154 ret = phy_speed_up(pl->phydev);
2155
2156 return ret;
2157}
2158EXPORT_SYMBOL_GPL(phylink_speed_up);
2159
Russell King320587e62019-05-28 10:57:34 +01002160static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2161{
2162 struct phylink *pl = upstream;
2163
2164 pl->netdev->sfp_bus = bus;
2165}
2166
2167static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2168{
2169 struct phylink *pl = upstream;
2170
2171 pl->netdev->sfp_bus = NULL;
2172}
2173
Russell King52c95602019-12-11 10:56:45 +00002174static int phylink_sfp_config(struct phylink *pl, u8 mode,
Russell Kingc0de2f42019-12-11 10:56:40 +00002175 const unsigned long *supported,
2176 const unsigned long *advertising)
Russell Kingce0aa272017-07-25 15:03:18 +01002177{
Russell King77316762019-06-02 15:12:54 +01002178 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
Russell Kingc0de2f42019-12-11 10:56:40 +00002179 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
Russell Kingce0aa272017-07-25 15:03:18 +01002180 struct phylink_link_state config;
2181 phy_interface_t iface;
Russell Kingce0aa272017-07-25 15:03:18 +01002182 bool changed;
Russell Kingc0de2f42019-12-11 10:56:40 +00002183 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +01002184
Russell Kingc0de2f42019-12-11 10:56:40 +00002185 linkmode_copy(support, supported);
Russell Kingce0aa272017-07-25 15:03:18 +01002186
2187 memset(&config, 0, sizeof(config));
Russell Kingc0de2f42019-12-11 10:56:40 +00002188 linkmode_copy(config.advertising, advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00002189 config.interface = PHY_INTERFACE_MODE_NA;
Russell Kingce0aa272017-07-25 15:03:18 +01002190 config.speed = SPEED_UNKNOWN;
2191 config.duplex = DUPLEX_UNKNOWN;
2192 config.pause = MLO_PAUSE_AN;
2193 config.an_enabled = pl->link_config.an_enabled;
2194
2195 /* Ignore errors if we're expecting a PHY to attach later */
2196 ret = phylink_validate(pl, support, &config);
2197 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03002198 phylink_err(pl, "validation with support %*pb failed: %d\n",
2199 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kinga9c79362018-02-27 15:53:02 +00002200 return ret;
2201 }
2202
Russell Kinga4516c72019-12-11 10:55:59 +00002203 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00002204 if (iface == PHY_INTERFACE_MODE_NA) {
Ioana Ciornei170911802019-05-28 20:38:14 +03002205 phylink_err(pl,
2206 "selection of interface failed, advertisement %*pb\n",
2207 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00002208 return -EINVAL;
2209 }
2210
2211 config.interface = iface;
Russell Kingc0de2f42019-12-11 10:56:40 +00002212 linkmode_copy(support1, support);
Russell King77316762019-06-02 15:12:54 +01002213 ret = phylink_validate(pl, support1, &config);
Russell Kinga9c79362018-02-27 15:53:02 +00002214 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03002215 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
Russell Kingc0de2f42019-12-11 10:56:40 +00002216 phylink_an_mode_str(mode),
Ioana Ciornei170911802019-05-28 20:38:14 +03002217 phy_modes(config.interface),
2218 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kingce0aa272017-07-25 15:03:18 +01002219 return ret;
2220 }
2221
Ioana Ciornei170911802019-05-28 20:38:14 +03002222 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
Russell Kingc0de2f42019-12-11 10:56:40 +00002223 phylink_an_mode_str(mode), phy_modes(config.interface),
Ioana Ciornei170911802019-05-28 20:38:14 +03002224 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
Russell Kingce0aa272017-07-25 15:03:18 +01002225
Russell King86a362c2017-12-01 10:24:26 +00002226 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01002227 return -EINVAL;
2228
Nathan Rossiea269a62021-09-02 05:14:49 +00002229 changed = !linkmode_equal(pl->supported, support) ||
2230 !linkmode_equal(pl->link_config.advertising,
2231 config.advertising);
Russell Kingce0aa272017-07-25 15:03:18 +01002232 if (changed) {
2233 linkmode_copy(pl->supported, support);
2234 linkmode_copy(pl->link_config.advertising, config.advertising);
2235 }
2236
Russell Kingc0de2f42019-12-11 10:56:40 +00002237 if (pl->cur_link_an_mode != mode ||
Russell Kingce0aa272017-07-25 15:03:18 +01002238 pl->link_config.interface != config.interface) {
2239 pl->link_config.interface = config.interface;
Russell Kingc0de2f42019-12-11 10:56:40 +00002240 pl->cur_link_an_mode = mode;
Russell Kingce0aa272017-07-25 15:03:18 +01002241
2242 changed = true;
2243
Ioana Ciornei170911802019-05-28 20:38:14 +03002244 phylink_info(pl, "switched to %s/%s link mode\n",
Russell Kingc0de2f42019-12-11 10:56:40 +00002245 phylink_an_mode_str(mode),
Ioana Ciornei170911802019-05-28 20:38:14 +03002246 phy_modes(config.interface));
Russell Kingce0aa272017-07-25 15:03:18 +01002247 }
2248
Russell King52c95602019-12-11 10:56:45 +00002249 pl->link_port = pl->sfp_port;
Russell Kingce0aa272017-07-25 15:03:18 +01002250
2251 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2252 &pl->phylink_disable_state))
Russell King4c0d6d32020-03-30 18:44:55 +01002253 phylink_mac_initial_config(pl, false);
Russell Kingce0aa272017-07-25 15:03:18 +01002254
2255 return ret;
2256}
2257
Russell Kingc0de2f42019-12-11 10:56:40 +00002258static int phylink_sfp_module_insert(void *upstream,
2259 const struct sfp_eeprom_id *id)
2260{
2261 struct phylink *pl = upstream;
Russell King52c95602019-12-11 10:56:45 +00002262 unsigned long *support = pl->sfp_support;
Russell Kingc0de2f42019-12-11 10:56:40 +00002263
2264 ASSERT_RTNL();
2265
Russell King52c95602019-12-11 10:56:45 +00002266 linkmode_zero(support);
Russell Kingc0de2f42019-12-11 10:56:40 +00002267 sfp_parse_support(pl->sfp_bus, id, support);
Russell King52c95602019-12-11 10:56:45 +00002268 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
Russell Kingc0de2f42019-12-11 10:56:40 +00002269
Russell King52c95602019-12-11 10:56:45 +00002270 /* If this module may have a PHY connecting later, defer until later */
2271 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2272 if (pl->sfp_may_have_phy)
2273 return 0;
2274
2275 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
Russell Kingc0de2f42019-12-11 10:56:40 +00002276}
2277
Russell King48820572019-12-11 10:56:14 +00002278static int phylink_sfp_module_start(void *upstream)
2279{
2280 struct phylink *pl = upstream;
2281
2282 /* If this SFP module has a PHY, start the PHY now. */
Russell King52c95602019-12-11 10:56:45 +00002283 if (pl->phydev) {
Russell King48820572019-12-11 10:56:14 +00002284 phy_start(pl->phydev);
Russell King52c95602019-12-11 10:56:45 +00002285 return 0;
2286 }
Russell King48820572019-12-11 10:56:14 +00002287
Russell King52c95602019-12-11 10:56:45 +00002288 /* If the module may have a PHY but we didn't detect one we
2289 * need to configure the MAC here.
2290 */
2291 if (!pl->sfp_may_have_phy)
2292 return 0;
2293
2294 return phylink_sfp_config(pl, MLO_AN_INBAND,
2295 pl->sfp_support, pl->sfp_support);
Russell King48820572019-12-11 10:56:14 +00002296}
2297
2298static void phylink_sfp_module_stop(void *upstream)
2299{
2300 struct phylink *pl = upstream;
2301
2302 /* If this SFP module has a PHY, stop it. */
2303 if (pl->phydev)
2304 phy_stop(pl->phydev);
2305}
2306
Russell Kingce0aa272017-07-25 15:03:18 +01002307static void phylink_sfp_link_down(void *upstream)
2308{
2309 struct phylink *pl = upstream;
2310
Russell King8b874512017-12-15 16:09:47 +00002311 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01002312
Russell King87454b62019-02-11 15:04:24 +00002313 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
Russell Kingce0aa272017-07-25 15:03:18 +01002314}
2315
2316static void phylink_sfp_link_up(void *upstream)
2317{
2318 struct phylink *pl = upstream;
2319
Russell King8b874512017-12-15 16:09:47 +00002320 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01002321
2322 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2323 phylink_run_resolve(pl);
2324}
2325
Russell King7adb5b22019-12-11 10:56:50 +00002326/* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2327 * or 802.3z control word, so inband will not work.
2328 */
2329static bool phylink_phy_no_inband(struct phy_device *phy)
2330{
2331 return phy->is_c45 &&
2332 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2333}
2334
Russell Kingce0aa272017-07-25 15:03:18 +01002335static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2336{
Baruch Siach7e418372018-10-03 19:04:49 +03002337 struct phylink *pl = upstream;
Russell King52c95602019-12-11 10:56:45 +00002338 phy_interface_t interface;
Russell King7adb5b22019-12-11 10:56:50 +00002339 u8 mode;
Russell King938d44c2019-12-11 10:56:25 +00002340 int ret;
Baruch Siach7e418372018-10-03 19:04:49 +03002341
Russell King52c95602019-12-11 10:56:45 +00002342 /*
2343 * This is the new way of dealing with flow control for PHYs,
2344 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2345 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2346 * using our validate call to the MAC, we rely upon the MAC
2347 * clearing the bits from both supported and advertising fields.
2348 */
2349 phy_support_asym_pause(phy);
2350
Russell King7adb5b22019-12-11 10:56:50 +00002351 if (phylink_phy_no_inband(phy))
2352 mode = MLO_AN_PHY;
2353 else
2354 mode = MLO_AN_INBAND;
2355
Russell King52c95602019-12-11 10:56:45 +00002356 /* Do the initial configuration */
Russell King7adb5b22019-12-11 10:56:50 +00002357 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
Russell King52c95602019-12-11 10:56:45 +00002358 if (ret < 0)
2359 return ret;
2360
2361 interface = pl->link_config.interface;
2362 ret = phylink_attach_phy(pl, phy, interface);
Russell King938d44c2019-12-11 10:56:25 +00002363 if (ret < 0)
2364 return ret;
2365
Russell Kinge45d1f52019-12-11 10:56:30 +00002366 ret = phylink_bringup_phy(pl, phy, interface);
Russell King938d44c2019-12-11 10:56:25 +00002367 if (ret)
2368 phy_detach(phy);
2369
2370 return ret;
Russell Kingce0aa272017-07-25 15:03:18 +01002371}
2372
2373static void phylink_sfp_disconnect_phy(void *upstream)
2374{
2375 phylink_disconnect_phy(upstream);
2376}
2377
2378static const struct sfp_upstream_ops sfp_phylink_ops = {
Russell King320587e62019-05-28 10:57:34 +01002379 .attach = phylink_sfp_attach,
2380 .detach = phylink_sfp_detach,
Russell Kingce0aa272017-07-25 15:03:18 +01002381 .module_insert = phylink_sfp_module_insert,
Russell King48820572019-12-11 10:56:14 +00002382 .module_start = phylink_sfp_module_start,
2383 .module_stop = phylink_sfp_module_stop,
Russell Kingce0aa272017-07-25 15:03:18 +01002384 .link_up = phylink_sfp_link_up,
2385 .link_down = phylink_sfp_link_down,
2386 .connect_phy = phylink_sfp_connect_phy,
2387 .disconnect_phy = phylink_sfp_disconnect_phy,
2388};
2389
Russell King624c0f02018-08-09 15:38:38 +02002390/* Helpers for MAC drivers */
2391
2392/**
2393 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2394 * @state: a pointer to a &struct phylink_link_state
2395 *
2396 * Inspect the interface mode, advertising mask or forced speed and
2397 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2398 * the interface mode to suit. @state->interface is appropriately
2399 * updated, and the advertising mask has the "other" baseX_Full flag
2400 * cleared.
2401 */
2402void phylink_helper_basex_speed(struct phylink_link_state *state)
2403{
2404 if (phy_interface_mode_is_8023z(state->interface)) {
2405 bool want_2500 = state->an_enabled ?
2406 phylink_test(state->advertising, 2500baseX_Full) :
2407 state->speed == SPEED_2500;
2408
2409 if (want_2500) {
2410 phylink_clear(state->advertising, 1000baseX_Full);
2411 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2412 } else {
2413 phylink_clear(state->advertising, 2500baseX_Full);
2414 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2415 }
2416 }
2417}
2418EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2419
Russell King74db1c12020-03-17 14:52:36 +00002420static void phylink_decode_c37_word(struct phylink_link_state *state,
2421 uint16_t config_reg, int speed)
2422{
2423 bool tx_pause, rx_pause;
2424 int fd_bit;
2425
2426 if (speed == SPEED_2500)
2427 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2428 else
2429 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2430
2431 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2432
2433 if (linkmode_test_bit(fd_bit, state->advertising) &&
2434 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2435 state->speed = speed;
2436 state->duplex = DUPLEX_FULL;
2437 } else {
2438 /* negotiation failure */
2439 state->link = false;
2440 }
2441
2442 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2443 &tx_pause, &rx_pause);
2444
2445 if (tx_pause)
2446 state->pause |= MLO_PAUSE_TX;
2447 if (rx_pause)
2448 state->pause |= MLO_PAUSE_RX;
2449}
2450
2451static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2452 uint16_t config_reg)
2453{
2454 if (!(config_reg & LPA_SGMII_LINK)) {
2455 state->link = false;
2456 return;
2457 }
2458
2459 switch (config_reg & LPA_SGMII_SPD_MASK) {
2460 case LPA_SGMII_10:
2461 state->speed = SPEED_10;
2462 break;
2463 case LPA_SGMII_100:
2464 state->speed = SPEED_100;
2465 break;
2466 case LPA_SGMII_1000:
2467 state->speed = SPEED_1000;
2468 break;
2469 default:
2470 state->link = false;
2471 return;
2472 }
2473 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2474 state->duplex = DUPLEX_FULL;
2475 else
2476 state->duplex = DUPLEX_HALF;
2477}
2478
2479/**
Ioana Ciorneiafd62202020-08-30 11:33:58 +03002480 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2481 * @state: a pointer to a struct phylink_link_state.
2482 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2483 *
2484 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2485 * code word. Decode the USXGMII code word and populate the corresponding fields
2486 * (speed, duplex) into the phylink_link_state structure.
2487 */
2488void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2489 uint16_t lpa)
2490{
2491 switch (lpa & MDIO_USXGMII_SPD_MASK) {
2492 case MDIO_USXGMII_10:
2493 state->speed = SPEED_10;
2494 break;
2495 case MDIO_USXGMII_100:
2496 state->speed = SPEED_100;
2497 break;
2498 case MDIO_USXGMII_1000:
2499 state->speed = SPEED_1000;
2500 break;
2501 case MDIO_USXGMII_2500:
2502 state->speed = SPEED_2500;
2503 break;
2504 case MDIO_USXGMII_5000:
2505 state->speed = SPEED_5000;
2506 break;
2507 case MDIO_USXGMII_10G:
2508 state->speed = SPEED_10000;
2509 break;
2510 default:
2511 state->link = false;
2512 return;
2513 }
2514
2515 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2516 state->duplex = DUPLEX_FULL;
2517 else
2518 state->duplex = DUPLEX_HALF;
2519}
2520EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2521
2522/**
Russell King74db1c12020-03-17 14:52:36 +00002523 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2524 * @pcs: a pointer to a &struct mdio_device.
2525 * @state: a pointer to a &struct phylink_link_state.
2526 *
2527 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2528 * clause 37 negotiation and/or SGMII control.
2529 *
2530 * Read the MAC PCS state from the MII device configured in @config and
2531 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2532 * the phylink @state structure. This is suitable to be directly plugged
2533 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2534 * structure.
2535 */
2536void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2537 struct phylink_link_state *state)
2538{
2539 struct mii_bus *bus = pcs->bus;
2540 int addr = pcs->addr;
2541 int bmsr, lpa;
2542
2543 bmsr = mdiobus_read(bus, addr, MII_BMSR);
2544 lpa = mdiobus_read(bus, addr, MII_LPA);
2545 if (bmsr < 0 || lpa < 0) {
2546 state->link = false;
2547 return;
2548 }
2549
2550 state->link = !!(bmsr & BMSR_LSTATUS);
2551 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2552 if (!state->link)
2553 return;
2554
2555 switch (state->interface) {
2556 case PHY_INTERFACE_MODE_1000BASEX:
2557 phylink_decode_c37_word(state, lpa, SPEED_1000);
2558 break;
2559
2560 case PHY_INTERFACE_MODE_2500BASEX:
2561 phylink_decode_c37_word(state, lpa, SPEED_2500);
2562 break;
2563
2564 case PHY_INTERFACE_MODE_SGMII:
Ioana Ciornei29f02ee2020-08-30 11:33:59 +03002565 case PHY_INTERFACE_MODE_QSGMII:
Russell King74db1c12020-03-17 14:52:36 +00002566 phylink_decode_sgmii_word(state, lpa);
2567 break;
2568
2569 default:
2570 state->link = false;
2571 break;
2572 }
2573}
2574EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2575
2576/**
2577 * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2578 * advertisement
2579 * @pcs: a pointer to a &struct mdio_device.
Russell King0bd27402020-03-30 18:44:44 +01002580 * @interface: the PHY interface mode being configured
2581 * @advertising: the ethtool advertisement mask
Russell King74db1c12020-03-17 14:52:36 +00002582 *
2583 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2584 * clause 37 negotiation and/or SGMII control.
2585 *
2586 * Configure the clause 37 PCS advertisement as specified by @state. This
2587 * does not trigger a renegotiation; phylink will do that via the
2588 * mac_an_restart() method of the struct phylink_mac_ops structure.
2589 *
2590 * Returns negative error code on failure to configure the advertisement,
2591 * zero if no change has been made, or one if the advertisement has changed.
2592 */
2593int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
Russell King0bd27402020-03-30 18:44:44 +01002594 phy_interface_t interface,
2595 const unsigned long *advertising)
Russell King74db1c12020-03-17 14:52:36 +00002596{
2597 struct mii_bus *bus = pcs->bus;
2598 int addr = pcs->addr;
Russell King74db1c12020-03-17 14:52:36 +00002599 u16 adv;
2600
Russell King0bd27402020-03-30 18:44:44 +01002601 switch (interface) {
Russell King74db1c12020-03-17 14:52:36 +00002602 case PHY_INTERFACE_MODE_1000BASEX:
2603 case PHY_INTERFACE_MODE_2500BASEX:
2604 adv = ADVERTISE_1000XFULL;
2605 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
Russell King0bd27402020-03-30 18:44:44 +01002606 advertising))
Russell King74db1c12020-03-17 14:52:36 +00002607 adv |= ADVERTISE_1000XPAUSE;
2608 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
Russell King0bd27402020-03-30 18:44:44 +01002609 advertising))
Russell King74db1c12020-03-17 14:52:36 +00002610 adv |= ADVERTISE_1000XPSE_ASYM;
2611
Russell King (Oracle)078e0b52021-10-06 13:19:25 +01002612 return mdiobus_modify_changed(bus, addr, MII_ADVERTISE,
2613 0xffff, adv);
Russell King74db1c12020-03-17 14:52:36 +00002614
2615 case PHY_INTERFACE_MODE_SGMII:
Russell King (Oracle)078e0b52021-10-06 13:19:25 +01002616 return mdiobus_modify_changed(bus, addr, MII_ADVERTISE,
2617 0xffff, 0x0001);
Russell King74db1c12020-03-17 14:52:36 +00002618
2619 default:
2620 /* Nothing to do for other modes */
2621 return 0;
2622 }
2623}
2624EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2625
2626/**
Russell King93eaceb2020-07-21 12:04:51 +01002627 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
2628 * @pcs: a pointer to a &struct mdio_device.
2629 * @mode: link autonegotiation mode
2630 * @interface: the PHY interface mode being configured
2631 * @advertising: the ethtool advertisement mask
2632 *
2633 * Configure a Clause 22 PCS PHY with the appropriate negotiation
2634 * parameters for the @mode, @interface and @advertising parameters.
2635 * Returns negative error number on failure, zero if the advertisement
2636 * has not changed, or positive if there is a change.
2637 */
2638int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
2639 phy_interface_t interface,
2640 const unsigned long *advertising)
2641{
2642 bool changed;
2643 u16 bmcr;
2644 int ret;
2645
2646 ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface,
2647 advertising);
2648 if (ret < 0)
2649 return ret;
2650
2651 changed = ret > 0;
2652
Robert Hancockcd292962020-10-26 11:58:02 -06002653 /* Ensure ISOLATE bit is disabled */
Russell King93eaceb2020-07-21 12:04:51 +01002654 bmcr = mode == MLO_AN_INBAND ? BMCR_ANENABLE : 0;
2655 ret = mdiobus_modify(pcs->bus, pcs->addr, MII_BMCR,
Robert Hancockcd292962020-10-26 11:58:02 -06002656 BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
Russell King93eaceb2020-07-21 12:04:51 +01002657 if (ret < 0)
2658 return ret;
2659
2660 return changed ? 1 : 0;
2661}
2662EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
2663
2664/**
Russell King74db1c12020-03-17 14:52:36 +00002665 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2666 * @pcs: a pointer to a &struct mdio_device.
2667 *
2668 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2669 * clause 37 negotiation.
2670 *
2671 * Restart the clause 37 negotiation with the link partner. This is
2672 * suitable to be directly plugged into the mac_pcs_get_state() member
2673 * of the struct phylink_mac_ops structure.
2674 */
2675void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2676{
2677 struct mii_bus *bus = pcs->bus;
2678 int val, addr = pcs->addr;
2679
2680 val = mdiobus_read(bus, addr, MII_BMCR);
2681 if (val >= 0) {
2682 val |= BMCR_ANRESTART;
2683
2684 mdiobus_write(bus, addr, MII_BMCR, val);
2685 }
2686}
2687EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2688
Russell Kingb8679ef2020-03-17 14:52:41 +00002689void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2690 struct phylink_link_state *state)
2691{
2692 struct mii_bus *bus = pcs->bus;
2693 int addr = pcs->addr;
2694 int stat;
2695
Russell King90ce6652020-05-26 16:29:36 +01002696 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
Russell Kingb8679ef2020-03-17 14:52:41 +00002697 if (stat < 0) {
2698 state->link = false;
2699 return;
2700 }
2701
2702 state->link = !!(stat & MDIO_STAT1_LSTATUS);
2703 if (!state->link)
2704 return;
2705
2706 switch (state->interface) {
2707 case PHY_INTERFACE_MODE_10GBASER:
2708 state->speed = SPEED_10000;
2709 state->duplex = DUPLEX_FULL;
2710 break;
2711
2712 default:
2713 break;
2714 }
2715}
2716EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2717
Andrew Lunn5f857572019-01-21 19:10:19 +01002718MODULE_LICENSE("GPL v2");