blob: 6ce2e6c63e75ebacfc9ab21668fdb6006e34e6b8 [file] [log] [blame]
Andrew Lunn5f857572019-01-21 19:10:19 +01001// SPDX-License-Identifier: GPL-2.0
Russell King9525ae82017-07-25 15:03:13 +01002/*
3 * phylink models the MAC to optional PHY connection, supporting
4 * technologies such as SFP cages where the PHY is hot-pluggable.
5 *
6 * Copyright (C) 2015 Russell King
Russell King9525ae82017-07-25 15:03:13 +01007 */
8#include <linux/ethtool.h>
9#include <linux/export.h>
10#include <linux/gpio/consumer.h>
11#include <linux/netdevice.h>
12#include <linux/of.h>
13#include <linux/of_mdio.h>
14#include <linux/phy.h>
15#include <linux/phy_fixed.h>
16#include <linux/phylink.h>
17#include <linux/rtnetlink.h>
18#include <linux/spinlock.h>
Russell King9cd00a82018-05-10 13:17:31 -070019#include <linux/timer.h>
Russell King9525ae82017-07-25 15:03:13 +010020#include <linux/workqueue.h>
21
Russell Kingce0aa272017-07-25 15:03:18 +010022#include "sfp.h"
Russell King9525ae82017-07-25 15:03:13 +010023#include "swphy.h"
24
25#define SUPPORTED_INTERFACES \
26 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
27 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
28#define ADVERTISED_INTERFACES \
29 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
30 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
31
32enum {
33 PHYLINK_DISABLE_STOPPED,
Russell Kingce0aa272017-07-25 15:03:18 +010034 PHYLINK_DISABLE_LINK,
Russell King9525ae82017-07-25 15:03:13 +010035};
36
Russell King8796c892017-12-01 10:24:47 +000037/**
38 * struct phylink - internal data type for phylink
39 */
Russell King9525ae82017-07-25 15:03:13 +010040struct phylink {
Russell King8796c892017-12-01 10:24:47 +000041 /* private: */
Russell King9525ae82017-07-25 15:03:13 +010042 struct net_device *netdev;
43 const struct phylink_mac_ops *ops;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +030044 struct phylink_config *config;
Ioana Ciornei43de6192019-05-28 20:38:13 +030045 struct device *dev;
46 unsigned int old_link_state:1;
Russell King9525ae82017-07-25 15:03:13 +010047
48 unsigned long phylink_disable_state; /* bitmask of disables */
49 struct phy_device *phydev;
50 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
51 u8 link_an_mode; /* MLO_AN_xxx */
52 u8 link_port; /* The current non-phy ethtool port */
53 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
54
55 /* The link configuration settings */
56 struct phylink_link_state link_config;
Russell Kingc6787262019-05-28 10:27:21 +010057
58 /* The current settings */
59 phy_interface_t cur_interface;
60
Russell King9525ae82017-07-25 15:03:13 +010061 struct gpio_desc *link_gpio;
Russell King7b3b0e82019-05-28 10:57:23 +010062 unsigned int link_irq;
Russell King9cd00a82018-05-10 13:17:31 -070063 struct timer_list link_poll;
Florian Fainelli1ac63e32017-12-12 16:00:28 -080064 void (*get_fixed_state)(struct net_device *dev,
65 struct phylink_link_state *s);
Russell King9525ae82017-07-25 15:03:13 +010066
67 struct mutex state_mutex;
68 struct phylink_link_state phy_state;
69 struct work_struct resolve;
70
71 bool mac_link_dropped;
Russell Kingce0aa272017-07-25 15:03:18 +010072
73 struct sfp_bus *sfp_bus;
Russell King9525ae82017-07-25 15:03:13 +010074};
75
Ioana Ciornei170911802019-05-28 20:38:14 +030076#define phylink_printk(level, pl, fmt, ...) \
77 do { \
78 if ((pl)->config->type == PHYLINK_NETDEV) \
79 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
80 else if ((pl)->config->type == PHYLINK_DEV) \
81 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
82 } while (0)
83
84#define phylink_err(pl, fmt, ...) \
85 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
86#define phylink_warn(pl, fmt, ...) \
87 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
88#define phylink_info(pl, fmt, ...) \
89 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
Florian Fainelli9d68db52019-10-31 15:42:26 -070090#if defined(CONFIG_DYNAMIC_DEBUG)
Ioana Ciornei170911802019-05-28 20:38:14 +030091#define phylink_dbg(pl, fmt, ...) \
Florian Fainelli9d68db52019-10-31 15:42:26 -070092do { \
93 if ((pl)->config->type == PHYLINK_NETDEV) \
94 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
95 else if ((pl)->config->type == PHYLINK_DEV) \
96 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
97} while (0)
98#elif defined(DEBUG)
99#define phylink_dbg(pl, fmt, ...) \
Ioana Ciornei170911802019-05-28 20:38:14 +0300100 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
Florian Fainelli9d68db52019-10-31 15:42:26 -0700101#else
102#define phylink_dbg(pl, fmt, ...) \
103({ \
104 if (0) \
105 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
106})
107#endif
Ioana Ciornei170911802019-05-28 20:38:14 +0300108
Russell King8796c892017-12-01 10:24:47 +0000109/**
110 * phylink_set_port_modes() - set the port type modes in the ethtool mask
111 * @mask: ethtool link mode mask
112 *
113 * Sets all the port type modes in the ethtool mask. MAC drivers should
114 * use this in their 'validate' callback.
115 */
Russell King9525ae82017-07-25 15:03:13 +0100116void phylink_set_port_modes(unsigned long *mask)
117{
118 phylink_set(mask, TP);
119 phylink_set(mask, AUI);
120 phylink_set(mask, MII);
121 phylink_set(mask, FIBRE);
122 phylink_set(mask, BNC);
123 phylink_set(mask, Backplane);
124}
125EXPORT_SYMBOL_GPL(phylink_set_port_modes);
126
127static int phylink_is_empty_linkmode(const unsigned long *linkmode)
128{
129 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
130
131 phylink_set_port_modes(tmp);
132 phylink_set(tmp, Autoneg);
133 phylink_set(tmp, Pause);
134 phylink_set(tmp, Asym_Pause);
135
Russell King554032c2019-10-15 11:28:46 +0100136 return linkmode_subset(linkmode, tmp);
Russell King9525ae82017-07-25 15:03:13 +0100137}
138
139static const char *phylink_an_mode_str(unsigned int mode)
140{
141 static const char *modestr[] = {
142 [MLO_AN_PHY] = "phy",
143 [MLO_AN_FIXED] = "fixed",
Russell King86a362c2017-12-01 10:24:26 +0000144 [MLO_AN_INBAND] = "inband",
Russell King9525ae82017-07-25 15:03:13 +0100145 };
146
147 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
148}
149
150static int phylink_validate(struct phylink *pl, unsigned long *supported,
151 struct phylink_link_state *state)
152{
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300153 pl->ops->validate(pl->config, supported, state);
Russell King9525ae82017-07-25 15:03:13 +0100154
155 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
156}
157
Russell King8fa7b9b2017-12-01 10:25:09 +0000158static int phylink_parse_fixedlink(struct phylink *pl,
159 struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100160{
Russell King8fa7b9b2017-12-01 10:25:09 +0000161 struct fwnode_handle *fixed_node;
Russell King9525ae82017-07-25 15:03:13 +0100162 const struct phy_setting *s;
163 struct gpio_desc *desc;
Russell King9525ae82017-07-25 15:03:13 +0100164 u32 speed;
Russell King8fa7b9b2017-12-01 10:25:09 +0000165 int ret;
Russell King9525ae82017-07-25 15:03:13 +0100166
Russell King8fa7b9b2017-12-01 10:25:09 +0000167 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
Russell King9525ae82017-07-25 15:03:13 +0100168 if (fixed_node) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000169 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
Russell King9525ae82017-07-25 15:03:13 +0100170
171 pl->link_config.speed = speed;
172 pl->link_config.duplex = DUPLEX_HALF;
173
Russell King8fa7b9b2017-12-01 10:25:09 +0000174 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
Russell King9525ae82017-07-25 15:03:13 +0100175 pl->link_config.duplex = DUPLEX_FULL;
176
177 /* We treat the "pause" and "asym-pause" terminology as
178 * defining the link partner's ability. */
Russell King8fa7b9b2017-12-01 10:25:09 +0000179 if (fwnode_property_read_bool(fixed_node, "pause"))
Russell King9525ae82017-07-25 15:03:13 +0100180 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000181 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
Russell King9525ae82017-07-25 15:03:13 +0100182 pl->link_config.pause |= MLO_PAUSE_ASYM;
183
184 if (ret == 0) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000185 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
186 0, GPIOD_IN, "?");
Russell King9525ae82017-07-25 15:03:13 +0100187
188 if (!IS_ERR(desc))
189 pl->link_gpio = desc;
190 else if (desc == ERR_PTR(-EPROBE_DEFER))
191 ret = -EPROBE_DEFER;
192 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000193 fwnode_handle_put(fixed_node);
Russell King9525ae82017-07-25 15:03:13 +0100194
195 if (ret)
196 return ret;
197 } else {
Russell King8fa7b9b2017-12-01 10:25:09 +0000198 u32 prop[5];
199
200 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
201 NULL, 0);
202 if (ret != ARRAY_SIZE(prop)) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300203 phylink_err(pl, "broken fixed-link?\n");
Russell King9525ae82017-07-25 15:03:13 +0100204 return -EINVAL;
205 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000206
207 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
208 prop, ARRAY_SIZE(prop));
209 if (!ret) {
210 pl->link_config.duplex = prop[1] ?
Russell King9525ae82017-07-25 15:03:13 +0100211 DUPLEX_FULL : DUPLEX_HALF;
Russell King8fa7b9b2017-12-01 10:25:09 +0000212 pl->link_config.speed = prop[2];
213 if (prop[3])
Russell King9525ae82017-07-25 15:03:13 +0100214 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000215 if (prop[4])
Russell King9525ae82017-07-25 15:03:13 +0100216 pl->link_config.pause |= MLO_PAUSE_ASYM;
217 }
218 }
219
220 if (pl->link_config.speed > SPEED_1000 &&
221 pl->link_config.duplex != DUPLEX_FULL)
Ioana Ciornei170911802019-05-28 20:38:14 +0300222 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
223 pl->link_config.speed);
Russell King9525ae82017-07-25 15:03:13 +0100224
225 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
226 linkmode_copy(pl->link_config.advertising, pl->supported);
227 phylink_validate(pl, pl->supported, &pl->link_config);
228
229 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100230 pl->supported, true);
Russell King9525ae82017-07-25 15:03:13 +0100231 linkmode_zero(pl->supported);
232 phylink_set(pl->supported, MII);
René van Dorst8aace4f2019-07-27 11:40:11 +0200233 phylink_set(pl->supported, Pause);
234 phylink_set(pl->supported, Asym_Pause);
Russell King9525ae82017-07-25 15:03:13 +0100235 if (s) {
236 __set_bit(s->bit, pl->supported);
237 } else {
Ioana Ciornei170911802019-05-28 20:38:14 +0300238 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
239 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
240 pl->link_config.speed);
Russell King9525ae82017-07-25 15:03:13 +0100241 }
242
243 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
244 pl->supported);
245
246 pl->link_config.link = 1;
247 pl->link_config.an_complete = 1;
248
249 return 0;
250}
251
Russell King8fa7b9b2017-12-01 10:25:09 +0000252static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100253{
Russell King8fa7b9b2017-12-01 10:25:09 +0000254 struct fwnode_handle *dn;
Russell King9525ae82017-07-25 15:03:13 +0100255 const char *managed;
256
Russell King8fa7b9b2017-12-01 10:25:09 +0000257 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
258 if (dn || fwnode_property_present(fwnode, "fixed-link"))
Russell King9525ae82017-07-25 15:03:13 +0100259 pl->link_an_mode = MLO_AN_FIXED;
Russell King8fa7b9b2017-12-01 10:25:09 +0000260 fwnode_handle_put(dn);
Russell King9525ae82017-07-25 15:03:13 +0100261
Russell King8fa7b9b2017-12-01 10:25:09 +0000262 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
Russell King9525ae82017-07-25 15:03:13 +0100263 strcmp(managed, "in-band-status") == 0) {
264 if (pl->link_an_mode == MLO_AN_FIXED) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300265 phylink_err(pl,
266 "can't use both fixed-link and in-band-status\n");
Russell King9525ae82017-07-25 15:03:13 +0100267 return -EINVAL;
268 }
269
270 linkmode_zero(pl->supported);
271 phylink_set(pl->supported, MII);
272 phylink_set(pl->supported, Autoneg);
273 phylink_set(pl->supported, Asym_Pause);
274 phylink_set(pl->supported, Pause);
275 pl->link_config.an_enabled = true;
Russell King86a362c2017-12-01 10:24:26 +0000276 pl->link_an_mode = MLO_AN_INBAND;
Russell King9525ae82017-07-25 15:03:13 +0100277
278 switch (pl->link_config.interface) {
279 case PHY_INTERFACE_MODE_SGMII:
280 phylink_set(pl->supported, 10baseT_Half);
281 phylink_set(pl->supported, 10baseT_Full);
282 phylink_set(pl->supported, 100baseT_Half);
283 phylink_set(pl->supported, 100baseT_Full);
284 phylink_set(pl->supported, 1000baseT_Half);
285 phylink_set(pl->supported, 1000baseT_Full);
Russell King9525ae82017-07-25 15:03:13 +0100286 break;
287
288 case PHY_INTERFACE_MODE_1000BASEX:
289 phylink_set(pl->supported, 1000baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100290 break;
291
292 case PHY_INTERFACE_MODE_2500BASEX:
293 phylink_set(pl->supported, 2500baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100294 break;
295
Russell Kingda7c1862017-07-25 15:03:34 +0100296 case PHY_INTERFACE_MODE_10GKR:
297 phylink_set(pl->supported, 10baseT_Half);
298 phylink_set(pl->supported, 10baseT_Full);
299 phylink_set(pl->supported, 100baseT_Half);
300 phylink_set(pl->supported, 100baseT_Full);
301 phylink_set(pl->supported, 1000baseT_Half);
302 phylink_set(pl->supported, 1000baseT_Full);
303 phylink_set(pl->supported, 1000baseX_Full);
304 phylink_set(pl->supported, 10000baseKR_Full);
305 phylink_set(pl->supported, 10000baseCR_Full);
306 phylink_set(pl->supported, 10000baseSR_Full);
307 phylink_set(pl->supported, 10000baseLR_Full);
308 phylink_set(pl->supported, 10000baseLRM_Full);
309 phylink_set(pl->supported, 10000baseER_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100310 break;
311
Russell King9525ae82017-07-25 15:03:13 +0100312 default:
Ioana Ciornei170911802019-05-28 20:38:14 +0300313 phylink_err(pl,
314 "incorrect link mode %s for in-band status\n",
315 phy_modes(pl->link_config.interface));
Russell King9525ae82017-07-25 15:03:13 +0100316 return -EINVAL;
317 }
318
319 linkmode_copy(pl->link_config.advertising, pl->supported);
320
321 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300322 phylink_err(pl,
323 "failed to validate link configuration for in-band status\n");
Russell King9525ae82017-07-25 15:03:13 +0100324 return -EINVAL;
325 }
326 }
327
328 return 0;
329}
330
331static void phylink_mac_config(struct phylink *pl,
332 const struct phylink_link_state *state)
333{
Ioana Ciornei170911802019-05-28 20:38:14 +0300334 phylink_dbg(pl,
335 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
336 __func__, phylink_an_mode_str(pl->link_an_mode),
337 phy_modes(state->interface),
338 phy_speed_to_str(state->speed),
339 phy_duplex_to_str(state->duplex),
340 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
341 state->pause, state->link, state->an_enabled);
Russell King9525ae82017-07-25 15:03:13 +0100342
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300343 pl->ops->mac_config(pl->config, pl->link_an_mode, state);
Russell King9525ae82017-07-25 15:03:13 +0100344}
345
Russell King0946cf12019-02-11 11:46:01 +0000346static void phylink_mac_config_up(struct phylink *pl,
347 const struct phylink_link_state *state)
348{
349 if (state->link)
350 phylink_mac_config(pl, state);
351}
352
Russell King9525ae82017-07-25 15:03:13 +0100353static void phylink_mac_an_restart(struct phylink *pl)
354{
355 if (pl->link_config.an_enabled &&
Russell King365c1e62017-12-01 10:24:16 +0000356 phy_interface_mode_is_8023z(pl->link_config.interface))
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300357 pl->ops->mac_an_restart(pl->config);
Russell King9525ae82017-07-25 15:03:13 +0100358}
359
360static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
361{
Russell King9525ae82017-07-25 15:03:13 +0100362
363 linkmode_copy(state->advertising, pl->link_config.advertising);
364 linkmode_zero(state->lp_advertising);
365 state->interface = pl->link_config.interface;
366 state->an_enabled = pl->link_config.an_enabled;
Heiner Kallweitd25ed412019-02-26 19:29:22 +0100367 state->speed = SPEED_UNKNOWN;
368 state->duplex = DUPLEX_UNKNOWN;
369 state->pause = MLO_PAUSE_NONE;
370 state->an_complete = 0;
Russell King9525ae82017-07-25 15:03:13 +0100371 state->link = 1;
372
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300373 return pl->ops->mac_link_state(pl->config, state);
Russell King9525ae82017-07-25 15:03:13 +0100374}
375
376/* The fixed state is... fixed except for the link state,
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800377 * which may be determined by a GPIO or a callback.
Russell King9525ae82017-07-25 15:03:13 +0100378 */
379static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
380{
381 *state = pl->link_config;
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800382 if (pl->get_fixed_state)
383 pl->get_fixed_state(pl->netdev, state);
384 else if (pl->link_gpio)
Florian Fainellibb322a92018-05-10 13:17:29 -0700385 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
Russell King9525ae82017-07-25 15:03:13 +0100386}
387
388/* Flow control is resolved according to our and the link partners
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000389 * advertisements using the following drawn from the 802.3 specs:
Russell King9525ae82017-07-25 15:03:13 +0100390 * Local device Link partner
391 * Pause AsymDir Pause AsymDir Result
392 * 1 X 1 X TX+RX
Stefan Chulski63b2ed42019-09-05 19:46:18 +0300393 * 0 1 1 1 TX
394 * 1 1 0 1 RX
Russell King9525ae82017-07-25 15:03:13 +0100395 */
396static void phylink_resolve_flow(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700397 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100398{
399 int new_pause = 0;
400
401 if (pl->link_config.pause & MLO_PAUSE_AN) {
402 int pause = 0;
403
404 if (phylink_test(pl->link_config.advertising, Pause))
405 pause |= MLO_PAUSE_SYM;
406 if (phylink_test(pl->link_config.advertising, Asym_Pause))
407 pause |= MLO_PAUSE_ASYM;
408
409 pause &= state->pause;
410
411 if (pause & MLO_PAUSE_SYM)
412 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
413 else if (pause & MLO_PAUSE_ASYM)
414 new_pause = state->pause & MLO_PAUSE_SYM ?
Stefan Chulski63b2ed42019-09-05 19:46:18 +0300415 MLO_PAUSE_TX : MLO_PAUSE_RX;
Russell King9525ae82017-07-25 15:03:13 +0100416 } else {
417 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
418 }
419
420 state->pause &= ~MLO_PAUSE_TXRX_MASK;
421 state->pause |= new_pause;
422}
423
424static const char *phylink_pause_to_str(int pause)
425{
426 switch (pause & MLO_PAUSE_TXRX_MASK) {
427 case MLO_PAUSE_TX | MLO_PAUSE_RX:
428 return "rx/tx";
429 case MLO_PAUSE_TX:
430 return "tx";
431 case MLO_PAUSE_RX:
432 return "rx";
433 default:
434 return "off";
435 }
436}
437
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300438static void phylink_mac_link_up(struct phylink *pl,
439 struct phylink_link_state link_state)
440{
441 struct net_device *ndev = pl->netdev;
442
David S. Millerb4b12b02019-05-31 10:49:43 -0700443 pl->cur_interface = link_state.interface;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300444 pl->ops->mac_link_up(pl->config, pl->link_an_mode,
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300445 pl->phy_state.interface,
446 pl->phydev);
447
Ioana Ciornei43de6192019-05-28 20:38:13 +0300448 if (ndev)
449 netif_carrier_on(ndev);
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300450
Ioana Ciornei170911802019-05-28 20:38:14 +0300451 phylink_info(pl,
452 "Link is Up - %s/%s - flow control %s\n",
453 phy_speed_to_str(link_state.speed),
454 phy_duplex_to_str(link_state.duplex),
455 phylink_pause_to_str(link_state.pause));
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300456}
457
458static void phylink_mac_link_down(struct phylink *pl)
459{
460 struct net_device *ndev = pl->netdev;
461
Ioana Ciornei43de6192019-05-28 20:38:13 +0300462 if (ndev)
463 netif_carrier_off(ndev);
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300464 pl->ops->mac_link_down(pl->config, pl->link_an_mode,
David S. Millerb4b12b02019-05-31 10:49:43 -0700465 pl->cur_interface);
Ioana Ciornei170911802019-05-28 20:38:14 +0300466 phylink_info(pl, "Link is Down\n");
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300467}
468
Russell King9525ae82017-07-25 15:03:13 +0100469static void phylink_resolve(struct work_struct *w)
470{
471 struct phylink *pl = container_of(w, struct phylink, resolve);
472 struct phylink_link_state link_state;
473 struct net_device *ndev = pl->netdev;
Ioana Ciornei43de6192019-05-28 20:38:13 +0300474 int link_changed;
Russell King9525ae82017-07-25 15:03:13 +0100475
476 mutex_lock(&pl->state_mutex);
477 if (pl->phylink_disable_state) {
478 pl->mac_link_dropped = false;
479 link_state.link = false;
480 } else if (pl->mac_link_dropped) {
481 link_state.link = false;
482 } else {
483 switch (pl->link_an_mode) {
484 case MLO_AN_PHY:
485 link_state = pl->phy_state;
486 phylink_resolve_flow(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000487 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100488 break;
489
490 case MLO_AN_FIXED:
491 phylink_get_fixed_state(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000492 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100493 break;
494
Russell King86a362c2017-12-01 10:24:26 +0000495 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100496 phylink_get_mac_state(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100497
Russell King406cb0c2019-05-20 16:07:20 +0100498 /* If we have a phy, the "up" state is the union of
499 * both the PHY and the MAC */
500 if (pl->phydev)
501 link_state.link &= pl->phy_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100502
Russell King406cb0c2019-05-20 16:07:20 +0100503 /* Only update if the PHY link is up */
504 if (pl->phydev && pl->phy_state.link) {
505 link_state.interface = pl->phy_state.interface;
Russell King9525ae82017-07-25 15:03:13 +0100506
Russell King406cb0c2019-05-20 16:07:20 +0100507 /* If we have a PHY, we need to update with
508 * the pause mode bits. */
509 link_state.pause |= pl->phy_state.pause;
510 phylink_resolve_flow(pl, &link_state);
511 phylink_mac_config(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100512 }
513 break;
Russell King9525ae82017-07-25 15:03:13 +0100514 }
515 }
516
Ioana Ciornei43de6192019-05-28 20:38:13 +0300517 if (pl->netdev)
518 link_changed = (link_state.link != netif_carrier_ok(ndev));
519 else
520 link_changed = (link_state.link != pl->old_link_state);
521
522 if (link_changed) {
523 pl->old_link_state = link_state.link;
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300524 if (!link_state.link)
525 phylink_mac_link_down(pl);
526 else
527 phylink_mac_link_up(pl, link_state);
Russell King9525ae82017-07-25 15:03:13 +0100528 }
529 if (!link_state.link && pl->mac_link_dropped) {
530 pl->mac_link_dropped = false;
531 queue_work(system_power_efficient_wq, &pl->resolve);
532 }
533 mutex_unlock(&pl->state_mutex);
534}
535
536static void phylink_run_resolve(struct phylink *pl)
537{
538 if (!pl->phylink_disable_state)
539 queue_work(system_power_efficient_wq, &pl->resolve);
540}
541
Russell King87454b62019-02-11 15:04:24 +0000542static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
543{
544 unsigned long state = pl->phylink_disable_state;
545
546 set_bit(bit, &pl->phylink_disable_state);
547 if (state == 0) {
548 queue_work(system_power_efficient_wq, &pl->resolve);
549 flush_work(&pl->resolve);
550 }
551}
552
Russell King9cd00a82018-05-10 13:17:31 -0700553static void phylink_fixed_poll(struct timer_list *t)
554{
555 struct phylink *pl = container_of(t, struct phylink, link_poll);
556
557 mod_timer(t, jiffies + HZ);
558
559 phylink_run_resolve(pl);
560}
561
Russell Kingce0aa272017-07-25 15:03:18 +0100562static const struct sfp_upstream_ops sfp_phylink_ops;
563
Russell King8fa7b9b2017-12-01 10:25:09 +0000564static int phylink_register_sfp(struct phylink *pl,
565 struct fwnode_handle *fwnode)
Russell Kingce0aa272017-07-25 15:03:18 +0100566{
Russell King2203cbf2019-10-15 11:38:39 +0100567 struct sfp_bus *bus;
Russell King8fa7b9b2017-12-01 10:25:09 +0000568 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100569
Russell King727b3662019-11-08 17:39:29 +0000570 bus = sfp_bus_find_fwnode(fwnode);
Russell King2203cbf2019-10-15 11:38:39 +0100571 if (IS_ERR(bus)) {
572 ret = PTR_ERR(bus);
573 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
Russell King8fa7b9b2017-12-01 10:25:09 +0000574 return ret;
575 }
576
Russell King2203cbf2019-10-15 11:38:39 +0100577 pl->sfp_bus = bus;
Russell Kingce0aa272017-07-25 15:03:18 +0100578
Russell King727b3662019-11-08 17:39:29 +0000579 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
580 sfp_bus_put(bus);
581
582 return ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100583}
584
Russell King8796c892017-12-01 10:24:47 +0000585/**
586 * phylink_create() - create a phylink instance
Randy Dunlap9db74e52019-10-08 08:39:04 -0700587 * @config: a pointer to the target &struct phylink_config
Russell King8fa7b9b2017-12-01 10:25:09 +0000588 * @fwnode: a pointer to a &struct fwnode_handle describing the network
589 * interface
Russell King8796c892017-12-01 10:24:47 +0000590 * @iface: the desired link mode defined by &typedef phy_interface_t
591 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
592 *
593 * Create a new phylink instance, and parse the link parameters found in @np.
594 * This will parse in-band modes, fixed-link or SFP configuration.
595 *
596 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
597 * must use IS_ERR() to check for errors from this function.
598 */
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300599struct phylink *phylink_create(struct phylink_config *config,
Russell King8fa7b9b2017-12-01 10:25:09 +0000600 struct fwnode_handle *fwnode,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700601 phy_interface_t iface,
602 const struct phylink_mac_ops *ops)
Russell King9525ae82017-07-25 15:03:13 +0100603{
604 struct phylink *pl;
605 int ret;
606
607 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
608 if (!pl)
609 return ERR_PTR(-ENOMEM);
610
611 mutex_init(&pl->state_mutex);
612 INIT_WORK(&pl->resolve, phylink_resolve);
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300613
614 pl->config = config;
615 if (config->type == PHYLINK_NETDEV) {
616 pl->netdev = to_net_dev(config->dev);
Ioana Ciornei43de6192019-05-28 20:38:13 +0300617 } else if (config->type == PHYLINK_DEV) {
618 pl->dev = config->dev;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300619 } else {
620 kfree(pl);
621 return ERR_PTR(-EINVAL);
622 }
623
Russell King9525ae82017-07-25 15:03:13 +0100624 pl->phy_state.interface = iface;
625 pl->link_interface = iface;
Florian Fainelli4be11ef2017-12-12 16:00:29 -0800626 if (iface == PHY_INTERFACE_MODE_MOCA)
627 pl->link_port = PORT_BNC;
628 else
629 pl->link_port = PORT_MII;
Russell King9525ae82017-07-25 15:03:13 +0100630 pl->link_config.interface = iface;
631 pl->link_config.pause = MLO_PAUSE_AN;
632 pl->link_config.speed = SPEED_UNKNOWN;
633 pl->link_config.duplex = DUPLEX_UNKNOWN;
Russell King74ee0e82017-12-20 23:21:34 +0000634 pl->link_config.an_enabled = true;
Russell King9525ae82017-07-25 15:03:13 +0100635 pl->ops = ops;
636 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King9cd00a82018-05-10 13:17:31 -0700637 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
Russell King9525ae82017-07-25 15:03:13 +0100638
639 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
640 linkmode_copy(pl->link_config.advertising, pl->supported);
641 phylink_validate(pl, pl->supported, &pl->link_config);
642
Russell King8fa7b9b2017-12-01 10:25:09 +0000643 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100644 if (ret < 0) {
645 kfree(pl);
646 return ERR_PTR(ret);
647 }
648
649 if (pl->link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000650 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100651 if (ret < 0) {
652 kfree(pl);
653 return ERR_PTR(ret);
654 }
655 }
656
Russell King8fa7b9b2017-12-01 10:25:09 +0000657 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100658 if (ret < 0) {
659 kfree(pl);
660 return ERR_PTR(ret);
661 }
662
Russell King9525ae82017-07-25 15:03:13 +0100663 return pl;
664}
665EXPORT_SYMBOL_GPL(phylink_create);
666
Russell King8796c892017-12-01 10:24:47 +0000667/**
668 * phylink_destroy() - cleanup and destroy the phylink instance
669 * @pl: a pointer to a &struct phylink returned from phylink_create()
670 *
671 * Destroy a phylink instance. Any PHY that has been attached must have been
672 * cleaned up via phylink_disconnect_phy() prior to calling this function.
673 */
Russell King9525ae82017-07-25 15:03:13 +0100674void phylink_destroy(struct phylink *pl)
675{
Russell King727b3662019-11-08 17:39:29 +0000676 sfp_bus_del_upstream(pl->sfp_bus);
Russell King7b3b0e82019-05-28 10:57:23 +0100677 if (pl->link_gpio)
Florian Fainellidaab3342018-05-10 13:17:30 -0700678 gpiod_put(pl->link_gpio);
Russell Kingce0aa272017-07-25 15:03:18 +0100679
Russell King9525ae82017-07-25 15:03:13 +0100680 cancel_work_sync(&pl->resolve);
681 kfree(pl);
682}
683EXPORT_SYMBOL_GPL(phylink_destroy);
684
Wei Yongjun1ec6e532017-11-02 11:14:48 +0000685static void phylink_phy_change(struct phy_device *phydev, bool up,
686 bool do_carrier)
Russell King9525ae82017-07-25 15:03:13 +0100687{
688 struct phylink *pl = phydev->phylink;
689
690 mutex_lock(&pl->state_mutex);
691 pl->phy_state.speed = phydev->speed;
692 pl->phy_state.duplex = phydev->duplex;
693 pl->phy_state.pause = MLO_PAUSE_NONE;
694 if (phydev->pause)
695 pl->phy_state.pause |= MLO_PAUSE_SYM;
696 if (phydev->asym_pause)
697 pl->phy_state.pause |= MLO_PAUSE_ASYM;
698 pl->phy_state.interface = phydev->interface;
699 pl->phy_state.link = up;
700 mutex_unlock(&pl->state_mutex);
701
702 phylink_run_resolve(pl);
703
Ioana Ciornei170911802019-05-28 20:38:14 +0300704 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
705 phy_modes(phydev->interface),
706 phy_speed_to_str(phydev->speed),
707 phy_duplex_to_str(phydev->duplex));
Russell King9525ae82017-07-25 15:03:13 +0100708}
709
710static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
711{
712 struct phylink_link_state config;
713 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
Russell King9525ae82017-07-25 15:03:13 +0100714 int ret;
715
Russell King9525ae82017-07-25 15:03:13 +0100716 /*
717 * This is the new way of dealing with flow control for PHYs,
718 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
719 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
720 * using our validate call to the MAC, we rely upon the MAC
721 * clearing the bits from both supported and advertising fields.
722 */
Russell King725ea4b2019-11-15 20:05:45 +0000723 phy_support_asym_pause(phy);
724
725 memset(&config, 0, sizeof(config));
726 linkmode_copy(supported, phy->supported);
727 linkmode_copy(config.advertising, phy->advertising);
728 config.interface = pl->link_config.interface;
Russell King9525ae82017-07-25 15:03:13 +0100729
730 ret = phylink_validate(pl, supported, &config);
731 if (ret)
732 return ret;
733
734 phy->phylink = pl;
735 phy->phy_link_change = phylink_phy_change;
736
Ioana Ciornei170911802019-05-28 20:38:14 +0300737 phylink_info(pl,
738 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
739 phy->drv->name);
Russell King9525ae82017-07-25 15:03:13 +0100740
741 mutex_lock(&phy->lock);
742 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100743 pl->phydev = phy;
744 linkmode_copy(pl->supported, supported);
745 linkmode_copy(pl->link_config.advertising, config.advertising);
746
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000747 /* Restrict the phy advertisement according to the MAC support. */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100748 linkmode_copy(phy->advertising, config.advertising);
Russell King9525ae82017-07-25 15:03:13 +0100749 mutex_unlock(&pl->state_mutex);
750 mutex_unlock(&phy->lock);
751
Ioana Ciornei170911802019-05-28 20:38:14 +0300752 phylink_dbg(pl,
753 "phy: setting supported %*pb advertising %*pb\n",
754 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
755 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100756
Heiner Kallweit434a4312019-01-23 07:31:58 +0100757 if (phy_interrupt_is_valid(phy))
758 phy_request_interrupt(phy);
Russell King9525ae82017-07-25 15:03:13 +0100759
760 return 0;
761}
762
Baruch Siach7e418372018-10-03 19:04:49 +0300763static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
764 phy_interface_t interface)
765{
766 int ret;
767
768 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
769 (pl->link_an_mode == MLO_AN_INBAND &&
770 phy_interface_mode_is_8023z(interface))))
771 return -EINVAL;
772
773 if (pl->phydev)
774 return -EBUSY;
775
776 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
777 if (ret)
778 return ret;
779
780 ret = phylink_bringup_phy(pl, phy);
781 if (ret)
782 phy_detach(phy);
783
784 return ret;
785}
786
Russell King8796c892017-12-01 10:24:47 +0000787/**
788 * phylink_connect_phy() - connect a PHY to the phylink instance
789 * @pl: a pointer to a &struct phylink returned from phylink_create()
790 * @phy: a pointer to a &struct phy_device.
791 *
792 * Connect @phy to the phylink instance specified by @pl by calling
793 * phy_attach_direct(). Configure the @phy according to the MAC driver's
794 * capabilities, start the PHYLIB state machine and enable any interrupts
795 * that the PHY supports.
796 *
797 * This updates the phylink's ethtool supported and advertising link mode
798 * masks.
799 *
800 * Returns 0 on success or a negative errno.
801 */
Russell King9525ae82017-07-25 15:03:13 +0100802int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
803{
Florian Fainelli4904b6e2017-12-12 16:00:26 -0800804 /* Use PHY device/driver interface */
805 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
806 pl->link_interface = phy->interface;
807 pl->link_config.interface = pl->link_interface;
808 }
809
Baruch Siach7e418372018-10-03 19:04:49 +0300810 return __phylink_connect_phy(pl, phy, pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100811}
812EXPORT_SYMBOL_GPL(phylink_connect_phy);
813
Russell King8796c892017-12-01 10:24:47 +0000814/**
815 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
816 * @pl: a pointer to a &struct phylink returned from phylink_create()
817 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -0800818 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +0000819 *
820 * Connect the phy specified in the device node @dn to the phylink instance
821 * specified by @pl. Actions specified in phylink_connect_phy() will be
822 * performed.
823 *
824 * Returns 0 on success or a negative errno.
825 */
Florian Fainelli0a629642017-12-12 16:00:25 -0800826int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
827 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +0100828{
829 struct device_node *phy_node;
830 struct phy_device *phy_dev;
831 int ret;
832
Russell Kingcf4f2672017-12-01 10:24:21 +0000833 /* Fixed links and 802.3z are handled without needing a PHY */
834 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000835 (pl->link_an_mode == MLO_AN_INBAND &&
836 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100837 return 0;
838
839 phy_node = of_parse_phandle(dn, "phy-handle", 0);
840 if (!phy_node)
841 phy_node = of_parse_phandle(dn, "phy", 0);
842 if (!phy_node)
843 phy_node = of_parse_phandle(dn, "phy-device", 0);
844
845 if (!phy_node) {
Florian Fainellid38b4afd2017-12-12 16:00:27 -0800846 if (pl->link_an_mode == MLO_AN_PHY)
Russell King9525ae82017-07-25 15:03:13 +0100847 return -ENODEV;
Russell King9525ae82017-07-25 15:03:13 +0100848 return 0;
849 }
850
Florian Fainelli0a629642017-12-12 16:00:25 -0800851 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
852 pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100853 /* We're done with the phy_node handle */
854 of_node_put(phy_node);
855
856 if (!phy_dev)
857 return -ENODEV;
858
859 ret = phylink_bringup_phy(pl, phy_dev);
860 if (ret)
861 phy_detach(phy_dev);
862
863 return ret;
864}
865EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
866
Russell King8796c892017-12-01 10:24:47 +0000867/**
868 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
869 * instance.
870 * @pl: a pointer to a &struct phylink returned from phylink_create()
871 *
872 * Disconnect any current PHY from the phylink instance described by @pl.
873 */
Russell King9525ae82017-07-25 15:03:13 +0100874void phylink_disconnect_phy(struct phylink *pl)
875{
876 struct phy_device *phy;
877
Russell King8b874512017-12-15 16:09:47 +0000878 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100879
880 phy = pl->phydev;
881 if (phy) {
882 mutex_lock(&phy->lock);
883 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100884 pl->phydev = NULL;
885 mutex_unlock(&pl->state_mutex);
886 mutex_unlock(&phy->lock);
887 flush_work(&pl->resolve);
888
889 phy_disconnect(phy);
890 }
891}
892EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
893
Russell King8796c892017-12-01 10:24:47 +0000894/**
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800895 * phylink_fixed_state_cb() - allow setting a fixed link callback
896 * @pl: a pointer to a &struct phylink returned from phylink_create()
897 * @cb: callback to execute to determine the fixed link state.
898 *
899 * The MAC driver should call this driver when the state of its link
900 * can be determined through e.g: an out of band MMIO register.
901 */
902int phylink_fixed_state_cb(struct phylink *pl,
903 void (*cb)(struct net_device *dev,
904 struct phylink_link_state *state))
905{
906 /* It does not make sense to let the link be overriden unless we use
907 * MLO_AN_FIXED
908 */
909 if (pl->link_an_mode != MLO_AN_FIXED)
910 return -EINVAL;
911
912 mutex_lock(&pl->state_mutex);
913 pl->get_fixed_state = cb;
914 mutex_unlock(&pl->state_mutex);
915
916 return 0;
917}
918EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
919
920/**
Russell King8796c892017-12-01 10:24:47 +0000921 * phylink_mac_change() - notify phylink of a change in MAC state
922 * @pl: a pointer to a &struct phylink returned from phylink_create()
923 * @up: indicates whether the link is currently up.
924 *
925 * The MAC driver should call this driver when the state of its link
926 * changes (eg, link failure, new negotiation results, etc.)
927 */
Russell King9525ae82017-07-25 15:03:13 +0100928void phylink_mac_change(struct phylink *pl, bool up)
929{
930 if (!up)
931 pl->mac_link_dropped = true;
932 phylink_run_resolve(pl);
Ioana Ciornei170911802019-05-28 20:38:14 +0300933 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
Russell King9525ae82017-07-25 15:03:13 +0100934}
935EXPORT_SYMBOL_GPL(phylink_mac_change);
936
Russell King7b3b0e82019-05-28 10:57:23 +0100937static irqreturn_t phylink_link_handler(int irq, void *data)
938{
939 struct phylink *pl = data;
940
941 phylink_run_resolve(pl);
942
943 return IRQ_HANDLED;
944}
945
Russell King8796c892017-12-01 10:24:47 +0000946/**
947 * phylink_start() - start a phylink instance
948 * @pl: a pointer to a &struct phylink returned from phylink_create()
949 *
950 * Start the phylink instance specified by @pl, configuring the MAC for the
951 * desired link mode(s) and negotiation style. This should be called from the
952 * network device driver's &struct net_device_ops ndo_open() method.
953 */
Russell King9525ae82017-07-25 15:03:13 +0100954void phylink_start(struct phylink *pl)
955{
Russell King8b874512017-12-15 16:09:47 +0000956 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100957
Ioana Ciornei170911802019-05-28 20:38:14 +0300958 phylink_info(pl, "configuring for %s/%s link mode\n",
959 phylink_an_mode_str(pl->link_an_mode),
960 phy_modes(pl->link_config.interface));
Russell King9525ae82017-07-25 15:03:13 +0100961
Antoine Tenartaeeb2e82018-09-19 11:39:31 +0200962 /* Always set the carrier off */
Ioana Ciornei43de6192019-05-28 20:38:13 +0300963 if (pl->netdev)
964 netif_carrier_off(pl->netdev);
Antoine Tenartaeeb2e82018-09-19 11:39:31 +0200965
Russell King9525ae82017-07-25 15:03:13 +0100966 /* Apply the link configuration to the MAC when starting. This allows
967 * a fixed-link to start with the correct parameters, and also
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000968 * ensures that we set the appropriate advertisement for Serdes links.
Russell King9525ae82017-07-25 15:03:13 +0100969 */
970 phylink_resolve_flow(pl, &pl->link_config);
971 phylink_mac_config(pl, &pl->link_config);
972
Russell King85b43942017-12-01 10:24:42 +0000973 /* Restart autonegotiation if using 802.3z to ensure that the link
974 * parameters are properly negotiated. This is necessary for DSA
975 * switches using 802.3z negotiation to ensure they see our modes.
976 */
977 phylink_mac_an_restart(pl);
978
Russell King9525ae82017-07-25 15:03:13 +0100979 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
980 phylink_run_resolve(pl);
981
Russell King7b3b0e82019-05-28 10:57:23 +0100982 if (pl->link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
983 int irq = gpiod_to_irq(pl->link_gpio);
984
985 if (irq > 0) {
986 if (!request_irq(irq, phylink_link_handler,
987 IRQF_TRIGGER_RISING |
988 IRQF_TRIGGER_FALLING,
989 "netdev link", pl))
990 pl->link_irq = irq;
991 else
992 irq = 0;
993 }
994 if (irq <= 0)
995 mod_timer(&pl->link_poll, jiffies + HZ);
996 }
997 if (pl->link_an_mode == MLO_AN_FIXED && pl->get_fixed_state)
Russell King9cd00a82018-05-10 13:17:31 -0700998 mod_timer(&pl->link_poll, jiffies + HZ);
Russell King9525ae82017-07-25 15:03:13 +0100999 if (pl->phydev)
1000 phy_start(pl->phydev);
Arseny Solokhac7fa7f52019-07-24 20:31:39 +07001001 if (pl->sfp_bus)
1002 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +01001003}
1004EXPORT_SYMBOL_GPL(phylink_start);
1005
Russell King8796c892017-12-01 10:24:47 +00001006/**
1007 * phylink_stop() - stop a phylink instance
1008 * @pl: a pointer to a &struct phylink returned from phylink_create()
1009 *
1010 * Stop the phylink instance specified by @pl. This should be called from the
1011 * network device driver's &struct net_device_ops ndo_stop() method. The
1012 * network device's carrier state should not be changed prior to calling this
1013 * function.
1014 */
Russell King9525ae82017-07-25 15:03:13 +01001015void phylink_stop(struct phylink *pl)
1016{
Russell King8b874512017-12-15 16:09:47 +00001017 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001018
Russell Kingce0aa272017-07-25 15:03:18 +01001019 if (pl->sfp_bus)
1020 sfp_upstream_stop(pl->sfp_bus);
Arseny Solokhac7fa7f52019-07-24 20:31:39 +07001021 if (pl->phydev)
1022 phy_stop(pl->phydev);
Russell King7b3b0e82019-05-28 10:57:23 +01001023 del_timer_sync(&pl->link_poll);
1024 if (pl->link_irq) {
1025 free_irq(pl->link_irq, pl);
1026 pl->link_irq = 0;
1027 }
Russell King9525ae82017-07-25 15:03:13 +01001028
Russell King87454b62019-02-11 15:04:24 +00001029 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
Russell King9525ae82017-07-25 15:03:13 +01001030}
1031EXPORT_SYMBOL_GPL(phylink_stop);
1032
Russell King8796c892017-12-01 10:24:47 +00001033/**
1034 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1035 * @pl: a pointer to a &struct phylink returned from phylink_create()
1036 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1037 *
1038 * Read the wake on lan parameters from the PHY attached to the phylink
1039 * instance specified by @pl. If no PHY is currently attached, report no
1040 * support for wake on lan.
1041 */
Russell King9525ae82017-07-25 15:03:13 +01001042void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1043{
Russell King8b874512017-12-15 16:09:47 +00001044 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001045
1046 wol->supported = 0;
1047 wol->wolopts = 0;
1048
1049 if (pl->phydev)
1050 phy_ethtool_get_wol(pl->phydev, wol);
1051}
1052EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1053
Russell King8796c892017-12-01 10:24:47 +00001054/**
1055 * phylink_ethtool_set_wol() - set wake on lan parameters
1056 * @pl: a pointer to a &struct phylink returned from phylink_create()
1057 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1058 *
1059 * Set the wake on lan parameters for the PHY attached to the phylink
1060 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1061 * error.
1062 *
1063 * Returns zero on success or negative errno code.
1064 */
Russell King9525ae82017-07-25 15:03:13 +01001065int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1066{
1067 int ret = -EOPNOTSUPP;
1068
Russell King8b874512017-12-15 16:09:47 +00001069 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001070
1071 if (pl->phydev)
1072 ret = phy_ethtool_set_wol(pl->phydev, wol);
1073
1074 return ret;
1075}
1076EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1077
1078static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1079{
1080 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1081
1082 linkmode_zero(mask);
1083 phylink_set_port_modes(mask);
1084
1085 linkmode_and(dst, dst, mask);
1086 linkmode_or(dst, dst, b);
1087}
1088
1089static void phylink_get_ksettings(const struct phylink_link_state *state,
1090 struct ethtool_link_ksettings *kset)
1091{
1092 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1093 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1094 kset->base.speed = state->speed;
1095 kset->base.duplex = state->duplex;
1096 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1097 AUTONEG_DISABLE;
1098}
1099
Russell King8796c892017-12-01 10:24:47 +00001100/**
1101 * phylink_ethtool_ksettings_get() - get the current link settings
1102 * @pl: a pointer to a &struct phylink returned from phylink_create()
1103 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1104 *
1105 * Read the current link settings for the phylink instance specified by @pl.
1106 * This will be the link settings read from the MAC, PHY or fixed link
1107 * settings depending on the current negotiation mode.
1108 */
Russell King9525ae82017-07-25 15:03:13 +01001109int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001110 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001111{
1112 struct phylink_link_state link_state;
1113
Russell King8b874512017-12-15 16:09:47 +00001114 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001115
1116 if (pl->phydev) {
1117 phy_ethtool_ksettings_get(pl->phydev, kset);
1118 } else {
1119 kset->base.port = pl->link_port;
1120 }
1121
1122 linkmode_copy(kset->link_modes.supported, pl->supported);
1123
1124 switch (pl->link_an_mode) {
1125 case MLO_AN_FIXED:
1126 /* We are using fixed settings. Report these as the
1127 * current link settings - and note that these also
1128 * represent the supported speeds/duplex/pause modes.
1129 */
1130 phylink_get_fixed_state(pl, &link_state);
1131 phylink_get_ksettings(&link_state, kset);
1132 break;
1133
Russell King86a362c2017-12-01 10:24:26 +00001134 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001135 /* If there is a phy attached, then use the reported
1136 * settings from the phy with no modification.
1137 */
1138 if (pl->phydev)
1139 break;
1140
Russell King9525ae82017-07-25 15:03:13 +01001141 phylink_get_mac_state(pl, &link_state);
1142
1143 /* The MAC is reporting the link results from its own PCS
1144 * layer via in-band status. Report these as the current
1145 * link settings.
1146 */
1147 phylink_get_ksettings(&link_state, kset);
1148 break;
1149 }
1150
1151 return 0;
1152}
1153EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1154
Russell King8796c892017-12-01 10:24:47 +00001155/**
1156 * phylink_ethtool_ksettings_set() - set the link settings
1157 * @pl: a pointer to a &struct phylink returned from phylink_create()
1158 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1159 */
Russell King9525ae82017-07-25 15:03:13 +01001160int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001161 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001162{
Russell King77316762019-06-02 15:12:54 +01001163 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
Russell King9525ae82017-07-25 15:03:13 +01001164 struct ethtool_link_ksettings our_kset;
1165 struct phylink_link_state config;
1166 int ret;
1167
Russell King8b874512017-12-15 16:09:47 +00001168 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001169
1170 if (kset->base.autoneg != AUTONEG_DISABLE &&
1171 kset->base.autoneg != AUTONEG_ENABLE)
1172 return -EINVAL;
1173
Russell King77316762019-06-02 15:12:54 +01001174 linkmode_copy(support, pl->supported);
Russell King9525ae82017-07-25 15:03:13 +01001175 config = pl->link_config;
1176
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001177 /* Mask out unsupported advertisements */
Russell King9525ae82017-07-25 15:03:13 +01001178 linkmode_and(config.advertising, kset->link_modes.advertising,
Russell King77316762019-06-02 15:12:54 +01001179 support);
Russell King9525ae82017-07-25 15:03:13 +01001180
1181 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1182 if (kset->base.autoneg == AUTONEG_DISABLE) {
1183 const struct phy_setting *s;
1184
1185 /* Autonegotiation disabled, select a suitable speed and
1186 * duplex.
1187 */
1188 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
Russell King77316762019-06-02 15:12:54 +01001189 support, false);
Russell King9525ae82017-07-25 15:03:13 +01001190 if (!s)
1191 return -EINVAL;
1192
1193 /* If we have a fixed link (as specified by firmware), refuse
1194 * to change link parameters.
1195 */
1196 if (pl->link_an_mode == MLO_AN_FIXED &&
1197 (s->speed != pl->link_config.speed ||
1198 s->duplex != pl->link_config.duplex))
1199 return -EINVAL;
1200
1201 config.speed = s->speed;
1202 config.duplex = s->duplex;
1203 config.an_enabled = false;
1204
1205 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1206 } else {
1207 /* If we have a fixed link, refuse to enable autonegotiation */
1208 if (pl->link_an_mode == MLO_AN_FIXED)
1209 return -EINVAL;
1210
1211 config.speed = SPEED_UNKNOWN;
1212 config.duplex = DUPLEX_UNKNOWN;
1213 config.an_enabled = true;
1214
1215 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1216 }
1217
Russell King77316762019-06-02 15:12:54 +01001218 if (phylink_validate(pl, support, &config))
Russell King9525ae82017-07-25 15:03:13 +01001219 return -EINVAL;
1220
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001221 /* If autonegotiation is enabled, we must have an advertisement */
Russell King9525ae82017-07-25 15:03:13 +01001222 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1223 return -EINVAL;
1224
1225 our_kset = *kset;
1226 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1227 our_kset.base.speed = config.speed;
1228 our_kset.base.duplex = config.duplex;
1229
1230 /* If we have a PHY, configure the phy */
1231 if (pl->phydev) {
1232 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1233 if (ret)
1234 return ret;
1235 }
1236
1237 mutex_lock(&pl->state_mutex);
1238 /* Configure the MAC to match the new settings */
1239 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
Russell King182088a2017-12-20 23:21:28 +00001240 pl->link_config.interface = config.interface;
Russell King9525ae82017-07-25 15:03:13 +01001241 pl->link_config.speed = our_kset.base.speed;
1242 pl->link_config.duplex = our_kset.base.duplex;
1243 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1244
1245 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1246 phylink_mac_config(pl, &pl->link_config);
1247 phylink_mac_an_restart(pl);
1248 }
1249 mutex_unlock(&pl->state_mutex);
1250
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001251 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001252}
1253EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1254
Russell King8796c892017-12-01 10:24:47 +00001255/**
1256 * phylink_ethtool_nway_reset() - restart negotiation
1257 * @pl: a pointer to a &struct phylink returned from phylink_create()
1258 *
1259 * Restart negotiation for the phylink instance specified by @pl. This will
1260 * cause any attached phy to restart negotiation with the link partner, and
1261 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1262 * negotiation.
1263 *
1264 * Returns zero on success, or negative error code.
1265 */
Russell King9525ae82017-07-25 15:03:13 +01001266int phylink_ethtool_nway_reset(struct phylink *pl)
1267{
1268 int ret = 0;
1269
Russell King8b874512017-12-15 16:09:47 +00001270 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001271
1272 if (pl->phydev)
1273 ret = phy_restart_aneg(pl->phydev);
1274 phylink_mac_an_restart(pl);
1275
1276 return ret;
1277}
1278EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1279
Russell King8796c892017-12-01 10:24:47 +00001280/**
1281 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1282 * @pl: a pointer to a &struct phylink returned from phylink_create()
1283 * @pause: a pointer to a &struct ethtool_pauseparam
1284 */
Russell King9525ae82017-07-25 15:03:13 +01001285void phylink_ethtool_get_pauseparam(struct phylink *pl,
1286 struct ethtool_pauseparam *pause)
1287{
Russell King8b874512017-12-15 16:09:47 +00001288 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001289
1290 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1291 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1292 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1293}
1294EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1295
Russell King8796c892017-12-01 10:24:47 +00001296/**
1297 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1298 * @pl: a pointer to a &struct phylink returned from phylink_create()
1299 * @pause: a pointer to a &struct ethtool_pauseparam
1300 */
Russell King9525ae82017-07-25 15:03:13 +01001301int phylink_ethtool_set_pauseparam(struct phylink *pl,
1302 struct ethtool_pauseparam *pause)
1303{
1304 struct phylink_link_state *config = &pl->link_config;
1305
Russell King8b874512017-12-15 16:09:47 +00001306 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001307
1308 if (!phylink_test(pl->supported, Pause) &&
1309 !phylink_test(pl->supported, Asym_Pause))
1310 return -EOPNOTSUPP;
1311
1312 if (!phylink_test(pl->supported, Asym_Pause) &&
1313 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1314 return -EINVAL;
1315
1316 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1317
1318 if (pause->autoneg)
1319 config->pause |= MLO_PAUSE_AN;
1320 if (pause->rx_pause)
1321 config->pause |= MLO_PAUSE_RX;
1322 if (pause->tx_pause)
1323 config->pause |= MLO_PAUSE_TX;
1324
1325 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1326 switch (pl->link_an_mode) {
1327 case MLO_AN_PHY:
1328 /* Silently mark the carrier down, and then trigger a resolve */
Ioana Ciornei43de6192019-05-28 20:38:13 +03001329 if (pl->netdev)
1330 netif_carrier_off(pl->netdev);
Russell King9525ae82017-07-25 15:03:13 +01001331 phylink_run_resolve(pl);
1332 break;
1333
1334 case MLO_AN_FIXED:
1335 /* Should we allow fixed links to change against the config? */
1336 phylink_resolve_flow(pl, config);
1337 phylink_mac_config(pl, config);
1338 break;
1339
Russell King86a362c2017-12-01 10:24:26 +00001340 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001341 phylink_mac_config(pl, config);
1342 phylink_mac_an_restart(pl);
1343 break;
1344 }
1345 }
1346
1347 return 0;
1348}
1349EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1350
Russell King8796c892017-12-01 10:24:47 +00001351/**
1352 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1353 * counter
1354 * @pl: a pointer to a &struct phylink returned from phylink_create().
1355 *
1356 * Read the Energy Efficient Ethernet error counter from the PHY associated
1357 * with the phylink instance specified by @pl.
1358 *
1359 * Returns positive error counter value, or negative error code.
1360 */
Russell King9525ae82017-07-25 15:03:13 +01001361int phylink_get_eee_err(struct phylink *pl)
1362{
1363 int ret = 0;
1364
Russell King8b874512017-12-15 16:09:47 +00001365 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001366
1367 if (pl->phydev)
1368 ret = phy_get_eee_err(pl->phydev);
1369
1370 return ret;
1371}
1372EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1373
Russell King8796c892017-12-01 10:24:47 +00001374/**
Russell King86e58132019-02-11 11:46:06 +00001375 * phylink_init_eee() - init and check the EEE features
1376 * @pl: a pointer to a &struct phylink returned from phylink_create()
1377 * @clk_stop_enable: allow PHY to stop receive clock
1378 *
1379 * Must be called either with RTNL held or within mac_link_up()
1380 */
1381int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1382{
1383 int ret = -EOPNOTSUPP;
1384
1385 if (pl->phydev)
1386 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1387
1388 return ret;
1389}
1390EXPORT_SYMBOL_GPL(phylink_init_eee);
1391
1392/**
Russell King8796c892017-12-01 10:24:47 +00001393 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1394 * @pl: a pointer to a &struct phylink returned from phylink_create()
1395 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1396 */
Russell King9525ae82017-07-25 15:03:13 +01001397int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1398{
1399 int ret = -EOPNOTSUPP;
1400
Russell King8b874512017-12-15 16:09:47 +00001401 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001402
1403 if (pl->phydev)
1404 ret = phy_ethtool_get_eee(pl->phydev, eee);
1405
1406 return ret;
1407}
1408EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1409
Russell King8796c892017-12-01 10:24:47 +00001410/**
1411 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1412 * @pl: a pointer to a &struct phylink returned from phylink_create()
1413 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1414 */
Russell King9525ae82017-07-25 15:03:13 +01001415int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1416{
1417 int ret = -EOPNOTSUPP;
1418
Russell King8b874512017-12-15 16:09:47 +00001419 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001420
1421 if (pl->phydev)
1422 ret = phy_ethtool_set_eee(pl->phydev, eee);
1423
1424 return ret;
1425}
1426EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1427
1428/* This emulates MII registers for a fixed-mode phy operating as per the
1429 * passed in state. "aneg" defines if we report negotiation is possible.
1430 *
1431 * FIXME: should deal with negotiation state too.
1432 */
Russell King7fdc4552019-05-28 10:57:18 +01001433static int phylink_mii_emul_read(unsigned int reg,
1434 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +01001435{
1436 struct fixed_phy_status fs;
1437 int val;
1438
1439 fs.link = state->link;
1440 fs.speed = state->speed;
1441 fs.duplex = state->duplex;
1442 fs.pause = state->pause & MLO_PAUSE_SYM;
1443 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1444
1445 val = swphy_read_reg(reg, &fs);
1446 if (reg == MII_BMSR) {
1447 if (!state->an_complete)
1448 val &= ~BMSR_ANEGCOMPLETE;
Russell King9525ae82017-07-25 15:03:13 +01001449 }
1450 return val;
1451}
1452
Russell Kingecbd87b2017-07-25 15:03:28 +01001453static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1454 unsigned int reg)
1455{
1456 struct phy_device *phydev = pl->phydev;
1457 int prtad, devad;
1458
1459 if (mdio_phy_id_is_c45(phy_id)) {
1460 prtad = mdio_phy_id_prtad(phy_id);
1461 devad = mdio_phy_id_devad(phy_id);
1462 devad = MII_ADDR_C45 | devad << 16 | reg;
1463 } else if (phydev->is_c45) {
1464 switch (reg) {
1465 case MII_BMCR:
1466 case MII_BMSR:
1467 case MII_PHYSID1:
1468 case MII_PHYSID2:
1469 devad = __ffs(phydev->c45_ids.devices_in_package);
1470 break;
1471 case MII_ADVERTISE:
1472 case MII_LPA:
1473 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1474 return -EINVAL;
1475 devad = MDIO_MMD_AN;
1476 if (reg == MII_ADVERTISE)
1477 reg = MDIO_AN_ADVERTISE;
1478 else
1479 reg = MDIO_AN_LPA;
1480 break;
1481 default:
1482 return -EINVAL;
1483 }
1484 prtad = phy_id;
1485 devad = MII_ADDR_C45 | devad << 16 | reg;
1486 } else {
1487 prtad = phy_id;
1488 devad = reg;
1489 }
1490 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1491}
1492
1493static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1494 unsigned int reg, unsigned int val)
1495{
1496 struct phy_device *phydev = pl->phydev;
1497 int prtad, devad;
1498
1499 if (mdio_phy_id_is_c45(phy_id)) {
1500 prtad = mdio_phy_id_prtad(phy_id);
1501 devad = mdio_phy_id_devad(phy_id);
1502 devad = MII_ADDR_C45 | devad << 16 | reg;
1503 } else if (phydev->is_c45) {
1504 switch (reg) {
1505 case MII_BMCR:
1506 case MII_BMSR:
1507 case MII_PHYSID1:
1508 case MII_PHYSID2:
1509 devad = __ffs(phydev->c45_ids.devices_in_package);
1510 break;
1511 case MII_ADVERTISE:
1512 case MII_LPA:
1513 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1514 return -EINVAL;
1515 devad = MDIO_MMD_AN;
1516 if (reg == MII_ADVERTISE)
1517 reg = MDIO_AN_ADVERTISE;
1518 else
1519 reg = MDIO_AN_LPA;
1520 break;
1521 default:
1522 return -EINVAL;
1523 }
1524 prtad = phy_id;
1525 devad = MII_ADDR_C45 | devad << 16 | reg;
1526 } else {
1527 prtad = phy_id;
1528 devad = reg;
1529 }
1530
1531 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1532}
1533
Russell King9525ae82017-07-25 15:03:13 +01001534static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1535 unsigned int reg)
1536{
1537 struct phylink_link_state state;
1538 int val = 0xffff;
1539
Russell King9525ae82017-07-25 15:03:13 +01001540 switch (pl->link_an_mode) {
1541 case MLO_AN_FIXED:
1542 if (phy_id == 0) {
1543 phylink_get_fixed_state(pl, &state);
Russell King7fdc4552019-05-28 10:57:18 +01001544 val = phylink_mii_emul_read(reg, &state);
Russell King9525ae82017-07-25 15:03:13 +01001545 }
1546 break;
1547
1548 case MLO_AN_PHY:
1549 return -EOPNOTSUPP;
1550
Russell King86a362c2017-12-01 10:24:26 +00001551 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001552 if (phy_id == 0) {
1553 val = phylink_get_mac_state(pl, &state);
1554 if (val < 0)
1555 return val;
1556
Russell King7fdc4552019-05-28 10:57:18 +01001557 val = phylink_mii_emul_read(reg, &state);
Russell King9525ae82017-07-25 15:03:13 +01001558 }
1559 break;
1560 }
1561
1562 return val & 0xffff;
1563}
1564
1565static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1566 unsigned int reg, unsigned int val)
1567{
Russell King9525ae82017-07-25 15:03:13 +01001568 switch (pl->link_an_mode) {
1569 case MLO_AN_FIXED:
1570 break;
1571
1572 case MLO_AN_PHY:
1573 return -EOPNOTSUPP;
1574
Russell King86a362c2017-12-01 10:24:26 +00001575 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001576 break;
1577 }
1578
1579 return 0;
1580}
1581
Russell King8796c892017-12-01 10:24:47 +00001582/**
1583 * phylink_mii_ioctl() - generic mii ioctl interface
1584 * @pl: a pointer to a &struct phylink returned from phylink_create()
1585 * @ifr: a pointer to a &struct ifreq for socket ioctls
1586 * @cmd: ioctl cmd to execute
1587 *
1588 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1589 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1590 *
1591 * Returns: zero on success or negative error code.
1592 *
1593 * %SIOCGMIIPHY:
1594 * read register from the current PHY.
1595 * %SIOCGMIIREG:
1596 * read register from the specified PHY.
1597 * %SIOCSMIIREG:
1598 * set a register on the specified PHY.
1599 */
Russell King9525ae82017-07-25 15:03:13 +01001600int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1601{
Russell Kingecbd87b2017-07-25 15:03:28 +01001602 struct mii_ioctl_data *mii = if_mii(ifr);
1603 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001604
Russell King8b874512017-12-15 16:09:47 +00001605 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001606
Russell Kingecbd87b2017-07-25 15:03:28 +01001607 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001608 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001609 switch (cmd) {
1610 case SIOCGMIIPHY:
1611 mii->phy_id = pl->phydev->mdio.addr;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001612 /* fall through */
Russell King9525ae82017-07-25 15:03:13 +01001613
Russell Kingecbd87b2017-07-25 15:03:28 +01001614 case SIOCGMIIREG:
1615 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1616 if (ret >= 0) {
1617 mii->val_out = ret;
1618 ret = 0;
1619 }
1620 break;
Russell King9525ae82017-07-25 15:03:13 +01001621
Russell Kingecbd87b2017-07-25 15:03:28 +01001622 case SIOCSMIIREG:
1623 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1624 mii->val_in);
1625 break;
Russell King9525ae82017-07-25 15:03:13 +01001626
Russell Kingecbd87b2017-07-25 15:03:28 +01001627 default:
Russell King9525ae82017-07-25 15:03:13 +01001628 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001629 break;
1630 }
1631 } else {
1632 switch (cmd) {
1633 case SIOCGMIIPHY:
1634 mii->phy_id = 0;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001635 /* fall through */
Russell Kingecbd87b2017-07-25 15:03:28 +01001636
1637 case SIOCGMIIREG:
1638 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1639 if (ret >= 0) {
1640 mii->val_out = ret;
1641 ret = 0;
1642 }
1643 break;
1644
1645 case SIOCSMIIREG:
1646 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1647 mii->val_in);
1648 break;
1649
1650 default:
1651 ret = -EOPNOTSUPP;
1652 break;
1653 }
Russell King9525ae82017-07-25 15:03:13 +01001654 }
1655
1656 return ret;
1657}
1658EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1659
Russell King320587e62019-05-28 10:57:34 +01001660static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
1661{
1662 struct phylink *pl = upstream;
1663
1664 pl->netdev->sfp_bus = bus;
1665}
1666
1667static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
1668{
1669 struct phylink *pl = upstream;
1670
1671 pl->netdev->sfp_bus = NULL;
1672}
1673
Russell Kingce0aa272017-07-25 15:03:18 +01001674static int phylink_sfp_module_insert(void *upstream,
1675 const struct sfp_eeprom_id *id)
1676{
1677 struct phylink *pl = upstream;
1678 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
Russell King77316762019-06-02 15:12:54 +01001679 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
Russell Kingce0aa272017-07-25 15:03:18 +01001680 struct phylink_link_state config;
1681 phy_interface_t iface;
Russell King444d3502017-12-29 12:15:33 +00001682 int ret = 0;
Russell Kingce0aa272017-07-25 15:03:18 +01001683 bool changed;
1684 u8 port;
1685
Russell King8b874512017-12-15 16:09:47 +00001686 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001687
Russell Kinga9c79362018-02-27 15:53:02 +00001688 sfp_parse_support(pl->sfp_bus, id, support);
1689 port = sfp_parse_port(pl->sfp_bus, id, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001690
1691 memset(&config, 0, sizeof(config));
1692 linkmode_copy(config.advertising, support);
Russell Kinga9c79362018-02-27 15:53:02 +00001693 config.interface = PHY_INTERFACE_MODE_NA;
Russell Kingce0aa272017-07-25 15:03:18 +01001694 config.speed = SPEED_UNKNOWN;
1695 config.duplex = DUPLEX_UNKNOWN;
1696 config.pause = MLO_PAUSE_AN;
1697 config.an_enabled = pl->link_config.an_enabled;
1698
1699 /* Ignore errors if we're expecting a PHY to attach later */
1700 ret = phylink_validate(pl, support, &config);
1701 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001702 phylink_err(pl, "validation with support %*pb failed: %d\n",
1703 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kinga9c79362018-02-27 15:53:02 +00001704 return ret;
1705 }
1706
Russell King77316762019-06-02 15:12:54 +01001707 linkmode_copy(support1, support);
1708
Russell Kinga9c79362018-02-27 15:53:02 +00001709 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1710 if (iface == PHY_INTERFACE_MODE_NA) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001711 phylink_err(pl,
1712 "selection of interface failed, advertisement %*pb\n",
1713 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00001714 return -EINVAL;
1715 }
1716
1717 config.interface = iface;
Russell King77316762019-06-02 15:12:54 +01001718 ret = phylink_validate(pl, support1, &config);
Russell Kinga9c79362018-02-27 15:53:02 +00001719 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001720 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
1721 phylink_an_mode_str(MLO_AN_INBAND),
1722 phy_modes(config.interface),
1723 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kingce0aa272017-07-25 15:03:18 +01001724 return ret;
1725 }
1726
Ioana Ciornei170911802019-05-28 20:38:14 +03001727 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
1728 phylink_an_mode_str(MLO_AN_INBAND),
1729 phy_modes(config.interface),
1730 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001731
Russell King86a362c2017-12-01 10:24:26 +00001732 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001733 return -EINVAL;
1734
Russell King554032c2019-10-15 11:28:46 +01001735 changed = !linkmode_equal(pl->supported, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001736 if (changed) {
1737 linkmode_copy(pl->supported, support);
1738 linkmode_copy(pl->link_config.advertising, config.advertising);
1739 }
1740
Russell King444d3502017-12-29 12:15:33 +00001741 if (pl->link_an_mode != MLO_AN_INBAND ||
Russell Kingce0aa272017-07-25 15:03:18 +01001742 pl->link_config.interface != config.interface) {
1743 pl->link_config.interface = config.interface;
Russell King444d3502017-12-29 12:15:33 +00001744 pl->link_an_mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001745
1746 changed = true;
1747
Ioana Ciornei170911802019-05-28 20:38:14 +03001748 phylink_info(pl, "switched to %s/%s link mode\n",
1749 phylink_an_mode_str(MLO_AN_INBAND),
1750 phy_modes(config.interface));
Russell Kingce0aa272017-07-25 15:03:18 +01001751 }
1752
1753 pl->link_port = port;
1754
1755 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1756 &pl->phylink_disable_state))
1757 phylink_mac_config(pl, &pl->link_config);
1758
1759 return ret;
1760}
1761
1762static void phylink_sfp_link_down(void *upstream)
1763{
1764 struct phylink *pl = upstream;
1765
Russell King8b874512017-12-15 16:09:47 +00001766 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001767
Russell King87454b62019-02-11 15:04:24 +00001768 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
Russell Kingce0aa272017-07-25 15:03:18 +01001769}
1770
1771static void phylink_sfp_link_up(void *upstream)
1772{
1773 struct phylink *pl = upstream;
1774
Russell King8b874512017-12-15 16:09:47 +00001775 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001776
1777 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1778 phylink_run_resolve(pl);
1779}
1780
1781static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1782{
Baruch Siach7e418372018-10-03 19:04:49 +03001783 struct phylink *pl = upstream;
1784
1785 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
Russell Kingce0aa272017-07-25 15:03:18 +01001786}
1787
1788static void phylink_sfp_disconnect_phy(void *upstream)
1789{
1790 phylink_disconnect_phy(upstream);
1791}
1792
1793static const struct sfp_upstream_ops sfp_phylink_ops = {
Russell King320587e62019-05-28 10:57:34 +01001794 .attach = phylink_sfp_attach,
1795 .detach = phylink_sfp_detach,
Russell Kingce0aa272017-07-25 15:03:18 +01001796 .module_insert = phylink_sfp_module_insert,
1797 .link_up = phylink_sfp_link_up,
1798 .link_down = phylink_sfp_link_down,
1799 .connect_phy = phylink_sfp_connect_phy,
1800 .disconnect_phy = phylink_sfp_disconnect_phy,
1801};
1802
Russell King624c0f02018-08-09 15:38:38 +02001803/* Helpers for MAC drivers */
1804
1805/**
1806 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1807 * @state: a pointer to a &struct phylink_link_state
1808 *
1809 * Inspect the interface mode, advertising mask or forced speed and
1810 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1811 * the interface mode to suit. @state->interface is appropriately
1812 * updated, and the advertising mask has the "other" baseX_Full flag
1813 * cleared.
1814 */
1815void phylink_helper_basex_speed(struct phylink_link_state *state)
1816{
1817 if (phy_interface_mode_is_8023z(state->interface)) {
1818 bool want_2500 = state->an_enabled ?
1819 phylink_test(state->advertising, 2500baseX_Full) :
1820 state->speed == SPEED_2500;
1821
1822 if (want_2500) {
1823 phylink_clear(state->advertising, 1000baseX_Full);
1824 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1825 } else {
1826 phylink_clear(state->advertising, 2500baseX_Full);
1827 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1828 }
1829 }
1830}
1831EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1832
Andrew Lunn5f857572019-01-21 19:10:19 +01001833MODULE_LICENSE("GPL v2");