blob: 68d0a89c52be1abe6b8ada2790974bdd41444e00 [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;
57 struct gpio_desc *link_gpio;
Russell King9cd00a82018-05-10 13:17:31 -070058 struct timer_list link_poll;
Florian Fainelli1ac63e32017-12-12 16:00:28 -080059 void (*get_fixed_state)(struct net_device *dev,
60 struct phylink_link_state *s);
Russell King9525ae82017-07-25 15:03:13 +010061
62 struct mutex state_mutex;
63 struct phylink_link_state phy_state;
64 struct work_struct resolve;
65
66 bool mac_link_dropped;
Russell Kingce0aa272017-07-25 15:03:18 +010067
68 struct sfp_bus *sfp_bus;
Russell King9525ae82017-07-25 15:03:13 +010069};
70
Ioana Ciornei170911802019-05-28 20:38:14 +030071#define phylink_printk(level, pl, fmt, ...) \
72 do { \
73 if ((pl)->config->type == PHYLINK_NETDEV) \
74 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
75 else if ((pl)->config->type == PHYLINK_DEV) \
76 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
77 } while (0)
78
79#define phylink_err(pl, fmt, ...) \
80 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
81#define phylink_warn(pl, fmt, ...) \
82 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
83#define phylink_info(pl, fmt, ...) \
84 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
85#define phylink_dbg(pl, fmt, ...) \
86 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
87
Russell King8796c892017-12-01 10:24:47 +000088/**
89 * phylink_set_port_modes() - set the port type modes in the ethtool mask
90 * @mask: ethtool link mode mask
91 *
92 * Sets all the port type modes in the ethtool mask. MAC drivers should
93 * use this in their 'validate' callback.
94 */
Russell King9525ae82017-07-25 15:03:13 +010095void phylink_set_port_modes(unsigned long *mask)
96{
97 phylink_set(mask, TP);
98 phylink_set(mask, AUI);
99 phylink_set(mask, MII);
100 phylink_set(mask, FIBRE);
101 phylink_set(mask, BNC);
102 phylink_set(mask, Backplane);
103}
104EXPORT_SYMBOL_GPL(phylink_set_port_modes);
105
106static int phylink_is_empty_linkmode(const unsigned long *linkmode)
107{
108 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
109
110 phylink_set_port_modes(tmp);
111 phylink_set(tmp, Autoneg);
112 phylink_set(tmp, Pause);
113 phylink_set(tmp, Asym_Pause);
114
115 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
116
117 return linkmode_empty(tmp);
118}
119
120static const char *phylink_an_mode_str(unsigned int mode)
121{
122 static const char *modestr[] = {
123 [MLO_AN_PHY] = "phy",
124 [MLO_AN_FIXED] = "fixed",
Russell King86a362c2017-12-01 10:24:26 +0000125 [MLO_AN_INBAND] = "inband",
Russell King9525ae82017-07-25 15:03:13 +0100126 };
127
128 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
129}
130
131static int phylink_validate(struct phylink *pl, unsigned long *supported,
132 struct phylink_link_state *state)
133{
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300134 pl->ops->validate(pl->config, supported, state);
Russell King9525ae82017-07-25 15:03:13 +0100135
136 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
137}
138
Russell King8fa7b9b2017-12-01 10:25:09 +0000139static int phylink_parse_fixedlink(struct phylink *pl,
140 struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100141{
Russell King8fa7b9b2017-12-01 10:25:09 +0000142 struct fwnode_handle *fixed_node;
Russell King9525ae82017-07-25 15:03:13 +0100143 const struct phy_setting *s;
144 struct gpio_desc *desc;
Russell King9525ae82017-07-25 15:03:13 +0100145 u32 speed;
Russell King8fa7b9b2017-12-01 10:25:09 +0000146 int ret;
Russell King9525ae82017-07-25 15:03:13 +0100147
Russell King8fa7b9b2017-12-01 10:25:09 +0000148 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
Russell King9525ae82017-07-25 15:03:13 +0100149 if (fixed_node) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000150 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
Russell King9525ae82017-07-25 15:03:13 +0100151
152 pl->link_config.speed = speed;
153 pl->link_config.duplex = DUPLEX_HALF;
154
Russell King8fa7b9b2017-12-01 10:25:09 +0000155 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
Russell King9525ae82017-07-25 15:03:13 +0100156 pl->link_config.duplex = DUPLEX_FULL;
157
158 /* We treat the "pause" and "asym-pause" terminology as
159 * defining the link partner's ability. */
Russell King8fa7b9b2017-12-01 10:25:09 +0000160 if (fwnode_property_read_bool(fixed_node, "pause"))
Russell King9525ae82017-07-25 15:03:13 +0100161 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000162 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
Russell King9525ae82017-07-25 15:03:13 +0100163 pl->link_config.pause |= MLO_PAUSE_ASYM;
164
165 if (ret == 0) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000166 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
167 0, GPIOD_IN, "?");
Russell King9525ae82017-07-25 15:03:13 +0100168
169 if (!IS_ERR(desc))
170 pl->link_gpio = desc;
171 else if (desc == ERR_PTR(-EPROBE_DEFER))
172 ret = -EPROBE_DEFER;
173 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000174 fwnode_handle_put(fixed_node);
Russell King9525ae82017-07-25 15:03:13 +0100175
176 if (ret)
177 return ret;
178 } else {
Russell King8fa7b9b2017-12-01 10:25:09 +0000179 u32 prop[5];
180
181 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
182 NULL, 0);
183 if (ret != ARRAY_SIZE(prop)) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300184 phylink_err(pl, "broken fixed-link?\n");
Russell King9525ae82017-07-25 15:03:13 +0100185 return -EINVAL;
186 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000187
188 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
189 prop, ARRAY_SIZE(prop));
190 if (!ret) {
191 pl->link_config.duplex = prop[1] ?
Russell King9525ae82017-07-25 15:03:13 +0100192 DUPLEX_FULL : DUPLEX_HALF;
Russell King8fa7b9b2017-12-01 10:25:09 +0000193 pl->link_config.speed = prop[2];
194 if (prop[3])
Russell King9525ae82017-07-25 15:03:13 +0100195 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000196 if (prop[4])
Russell King9525ae82017-07-25 15:03:13 +0100197 pl->link_config.pause |= MLO_PAUSE_ASYM;
198 }
199 }
200
201 if (pl->link_config.speed > SPEED_1000 &&
202 pl->link_config.duplex != DUPLEX_FULL)
Ioana Ciornei170911802019-05-28 20:38:14 +0300203 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
204 pl->link_config.speed);
Russell King9525ae82017-07-25 15:03:13 +0100205
206 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
207 linkmode_copy(pl->link_config.advertising, pl->supported);
208 phylink_validate(pl, pl->supported, &pl->link_config);
209
210 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100211 pl->supported, true);
Russell King9525ae82017-07-25 15:03:13 +0100212 linkmode_zero(pl->supported);
213 phylink_set(pl->supported, MII);
214 if (s) {
215 __set_bit(s->bit, pl->supported);
216 } else {
Ioana Ciornei170911802019-05-28 20:38:14 +0300217 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
218 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
219 pl->link_config.speed);
Russell King9525ae82017-07-25 15:03:13 +0100220 }
221
222 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
223 pl->supported);
224
225 pl->link_config.link = 1;
226 pl->link_config.an_complete = 1;
227
228 return 0;
229}
230
Russell King8fa7b9b2017-12-01 10:25:09 +0000231static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100232{
Russell King8fa7b9b2017-12-01 10:25:09 +0000233 struct fwnode_handle *dn;
Russell King9525ae82017-07-25 15:03:13 +0100234 const char *managed;
235
Russell King8fa7b9b2017-12-01 10:25:09 +0000236 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
237 if (dn || fwnode_property_present(fwnode, "fixed-link"))
Russell King9525ae82017-07-25 15:03:13 +0100238 pl->link_an_mode = MLO_AN_FIXED;
Russell King8fa7b9b2017-12-01 10:25:09 +0000239 fwnode_handle_put(dn);
Russell King9525ae82017-07-25 15:03:13 +0100240
Russell King8fa7b9b2017-12-01 10:25:09 +0000241 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
Russell King9525ae82017-07-25 15:03:13 +0100242 strcmp(managed, "in-band-status") == 0) {
243 if (pl->link_an_mode == MLO_AN_FIXED) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300244 phylink_err(pl,
245 "can't use both fixed-link and in-band-status\n");
Russell King9525ae82017-07-25 15:03:13 +0100246 return -EINVAL;
247 }
248
249 linkmode_zero(pl->supported);
250 phylink_set(pl->supported, MII);
251 phylink_set(pl->supported, Autoneg);
252 phylink_set(pl->supported, Asym_Pause);
253 phylink_set(pl->supported, Pause);
254 pl->link_config.an_enabled = true;
Russell King86a362c2017-12-01 10:24:26 +0000255 pl->link_an_mode = MLO_AN_INBAND;
Russell King9525ae82017-07-25 15:03:13 +0100256
257 switch (pl->link_config.interface) {
258 case PHY_INTERFACE_MODE_SGMII:
259 phylink_set(pl->supported, 10baseT_Half);
260 phylink_set(pl->supported, 10baseT_Full);
261 phylink_set(pl->supported, 100baseT_Half);
262 phylink_set(pl->supported, 100baseT_Full);
263 phylink_set(pl->supported, 1000baseT_Half);
264 phylink_set(pl->supported, 1000baseT_Full);
Russell King9525ae82017-07-25 15:03:13 +0100265 break;
266
267 case PHY_INTERFACE_MODE_1000BASEX:
268 phylink_set(pl->supported, 1000baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100269 break;
270
271 case PHY_INTERFACE_MODE_2500BASEX:
272 phylink_set(pl->supported, 2500baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100273 break;
274
Russell Kingda7c1862017-07-25 15:03:34 +0100275 case PHY_INTERFACE_MODE_10GKR:
276 phylink_set(pl->supported, 10baseT_Half);
277 phylink_set(pl->supported, 10baseT_Full);
278 phylink_set(pl->supported, 100baseT_Half);
279 phylink_set(pl->supported, 100baseT_Full);
280 phylink_set(pl->supported, 1000baseT_Half);
281 phylink_set(pl->supported, 1000baseT_Full);
282 phylink_set(pl->supported, 1000baseX_Full);
283 phylink_set(pl->supported, 10000baseKR_Full);
284 phylink_set(pl->supported, 10000baseCR_Full);
285 phylink_set(pl->supported, 10000baseSR_Full);
286 phylink_set(pl->supported, 10000baseLR_Full);
287 phylink_set(pl->supported, 10000baseLRM_Full);
288 phylink_set(pl->supported, 10000baseER_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100289 break;
290
Russell King9525ae82017-07-25 15:03:13 +0100291 default:
Ioana Ciornei170911802019-05-28 20:38:14 +0300292 phylink_err(pl,
293 "incorrect link mode %s for in-band status\n",
294 phy_modes(pl->link_config.interface));
Russell King9525ae82017-07-25 15:03:13 +0100295 return -EINVAL;
296 }
297
298 linkmode_copy(pl->link_config.advertising, pl->supported);
299
300 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
Ioana Ciornei170911802019-05-28 20:38:14 +0300301 phylink_err(pl,
302 "failed to validate link configuration for in-band status\n");
Russell King9525ae82017-07-25 15:03:13 +0100303 return -EINVAL;
304 }
305 }
306
307 return 0;
308}
309
310static void phylink_mac_config(struct phylink *pl,
311 const struct phylink_link_state *state)
312{
Ioana Ciornei170911802019-05-28 20:38:14 +0300313 phylink_dbg(pl,
314 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
315 __func__, phylink_an_mode_str(pl->link_an_mode),
316 phy_modes(state->interface),
317 phy_speed_to_str(state->speed),
318 phy_duplex_to_str(state->duplex),
319 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
320 state->pause, state->link, state->an_enabled);
Russell King9525ae82017-07-25 15:03:13 +0100321
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300322 pl->ops->mac_config(pl->config, pl->link_an_mode, state);
Russell King9525ae82017-07-25 15:03:13 +0100323}
324
Russell King0946cf12019-02-11 11:46:01 +0000325static void phylink_mac_config_up(struct phylink *pl,
326 const struct phylink_link_state *state)
327{
328 if (state->link)
329 phylink_mac_config(pl, state);
330}
331
Russell King9525ae82017-07-25 15:03:13 +0100332static void phylink_mac_an_restart(struct phylink *pl)
333{
334 if (pl->link_config.an_enabled &&
Russell King365c1e62017-12-01 10:24:16 +0000335 phy_interface_mode_is_8023z(pl->link_config.interface))
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300336 pl->ops->mac_an_restart(pl->config);
Russell King9525ae82017-07-25 15:03:13 +0100337}
338
339static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
340{
Russell King9525ae82017-07-25 15:03:13 +0100341
342 linkmode_copy(state->advertising, pl->link_config.advertising);
343 linkmode_zero(state->lp_advertising);
344 state->interface = pl->link_config.interface;
345 state->an_enabled = pl->link_config.an_enabled;
Heiner Kallweitd25ed412019-02-26 19:29:22 +0100346 state->speed = SPEED_UNKNOWN;
347 state->duplex = DUPLEX_UNKNOWN;
348 state->pause = MLO_PAUSE_NONE;
349 state->an_complete = 0;
Russell King9525ae82017-07-25 15:03:13 +0100350 state->link = 1;
351
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300352 return pl->ops->mac_link_state(pl->config, state);
Russell King9525ae82017-07-25 15:03:13 +0100353}
354
355/* The fixed state is... fixed except for the link state,
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800356 * which may be determined by a GPIO or a callback.
Russell King9525ae82017-07-25 15:03:13 +0100357 */
358static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
359{
360 *state = pl->link_config;
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800361 if (pl->get_fixed_state)
362 pl->get_fixed_state(pl->netdev, state);
363 else if (pl->link_gpio)
Florian Fainellibb322a92018-05-10 13:17:29 -0700364 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
Russell King9525ae82017-07-25 15:03:13 +0100365}
366
367/* Flow control is resolved according to our and the link partners
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000368 * advertisements using the following drawn from the 802.3 specs:
Russell King9525ae82017-07-25 15:03:13 +0100369 * Local device Link partner
370 * Pause AsymDir Pause AsymDir Result
371 * 1 X 1 X TX+RX
372 * 0 1 1 1 RX
373 * 1 1 0 1 TX
374 */
375static void phylink_resolve_flow(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700376 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100377{
378 int new_pause = 0;
379
380 if (pl->link_config.pause & MLO_PAUSE_AN) {
381 int pause = 0;
382
383 if (phylink_test(pl->link_config.advertising, Pause))
384 pause |= MLO_PAUSE_SYM;
385 if (phylink_test(pl->link_config.advertising, Asym_Pause))
386 pause |= MLO_PAUSE_ASYM;
387
388 pause &= state->pause;
389
390 if (pause & MLO_PAUSE_SYM)
391 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
392 else if (pause & MLO_PAUSE_ASYM)
393 new_pause = state->pause & MLO_PAUSE_SYM ?
394 MLO_PAUSE_RX : MLO_PAUSE_TX;
395 } else {
396 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
397 }
398
399 state->pause &= ~MLO_PAUSE_TXRX_MASK;
400 state->pause |= new_pause;
401}
402
403static const char *phylink_pause_to_str(int pause)
404{
405 switch (pause & MLO_PAUSE_TXRX_MASK) {
406 case MLO_PAUSE_TX | MLO_PAUSE_RX:
407 return "rx/tx";
408 case MLO_PAUSE_TX:
409 return "tx";
410 case MLO_PAUSE_RX:
411 return "rx";
412 default:
413 return "off";
414 }
415}
416
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300417static void phylink_mac_link_up(struct phylink *pl,
418 struct phylink_link_state link_state)
419{
420 struct net_device *ndev = pl->netdev;
421
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300422 pl->ops->mac_link_up(pl->config, pl->link_an_mode,
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300423 pl->phy_state.interface,
424 pl->phydev);
425
Ioana Ciornei43de6192019-05-28 20:38:13 +0300426 if (ndev)
427 netif_carrier_on(ndev);
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300428
Ioana Ciornei170911802019-05-28 20:38:14 +0300429 phylink_info(pl,
430 "Link is Up - %s/%s - flow control %s\n",
431 phy_speed_to_str(link_state.speed),
432 phy_duplex_to_str(link_state.duplex),
433 phylink_pause_to_str(link_state.pause));
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300434}
435
436static void phylink_mac_link_down(struct phylink *pl)
437{
438 struct net_device *ndev = pl->netdev;
439
Ioana Ciornei43de6192019-05-28 20:38:13 +0300440 if (ndev)
441 netif_carrier_off(ndev);
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300442 pl->ops->mac_link_down(pl->config, pl->link_an_mode,
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300443 pl->phy_state.interface);
Ioana Ciornei170911802019-05-28 20:38:14 +0300444 phylink_info(pl, "Link is Down\n");
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300445}
446
Russell King9525ae82017-07-25 15:03:13 +0100447static void phylink_resolve(struct work_struct *w)
448{
449 struct phylink *pl = container_of(w, struct phylink, resolve);
450 struct phylink_link_state link_state;
451 struct net_device *ndev = pl->netdev;
Ioana Ciornei43de6192019-05-28 20:38:13 +0300452 int link_changed;
Russell King9525ae82017-07-25 15:03:13 +0100453
454 mutex_lock(&pl->state_mutex);
455 if (pl->phylink_disable_state) {
456 pl->mac_link_dropped = false;
457 link_state.link = false;
458 } else if (pl->mac_link_dropped) {
459 link_state.link = false;
460 } else {
461 switch (pl->link_an_mode) {
462 case MLO_AN_PHY:
463 link_state = pl->phy_state;
464 phylink_resolve_flow(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000465 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100466 break;
467
468 case MLO_AN_FIXED:
469 phylink_get_fixed_state(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000470 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100471 break;
472
Russell King86a362c2017-12-01 10:24:26 +0000473 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100474 phylink_get_mac_state(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100475
Russell King406cb0c2019-05-20 16:07:20 +0100476 /* If we have a phy, the "up" state is the union of
477 * both the PHY and the MAC */
478 if (pl->phydev)
479 link_state.link &= pl->phy_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100480
Russell King406cb0c2019-05-20 16:07:20 +0100481 /* Only update if the PHY link is up */
482 if (pl->phydev && pl->phy_state.link) {
483 link_state.interface = pl->phy_state.interface;
Russell King9525ae82017-07-25 15:03:13 +0100484
Russell King406cb0c2019-05-20 16:07:20 +0100485 /* If we have a PHY, we need to update with
486 * the pause mode bits. */
487 link_state.pause |= pl->phy_state.pause;
488 phylink_resolve_flow(pl, &link_state);
489 phylink_mac_config(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100490 }
491 break;
Russell King9525ae82017-07-25 15:03:13 +0100492 }
493 }
494
Ioana Ciornei43de6192019-05-28 20:38:13 +0300495 if (pl->netdev)
496 link_changed = (link_state.link != netif_carrier_ok(ndev));
497 else
498 link_changed = (link_state.link != pl->old_link_state);
499
500 if (link_changed) {
501 pl->old_link_state = link_state.link;
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300502 if (!link_state.link)
503 phylink_mac_link_down(pl);
504 else
505 phylink_mac_link_up(pl, link_state);
Russell King9525ae82017-07-25 15:03:13 +0100506 }
507 if (!link_state.link && pl->mac_link_dropped) {
508 pl->mac_link_dropped = false;
509 queue_work(system_power_efficient_wq, &pl->resolve);
510 }
511 mutex_unlock(&pl->state_mutex);
512}
513
514static void phylink_run_resolve(struct phylink *pl)
515{
516 if (!pl->phylink_disable_state)
517 queue_work(system_power_efficient_wq, &pl->resolve);
518}
519
Russell King87454b62019-02-11 15:04:24 +0000520static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
521{
522 unsigned long state = pl->phylink_disable_state;
523
524 set_bit(bit, &pl->phylink_disable_state);
525 if (state == 0) {
526 queue_work(system_power_efficient_wq, &pl->resolve);
527 flush_work(&pl->resolve);
528 }
529}
530
Russell King9cd00a82018-05-10 13:17:31 -0700531static void phylink_fixed_poll(struct timer_list *t)
532{
533 struct phylink *pl = container_of(t, struct phylink, link_poll);
534
535 mod_timer(t, jiffies + HZ);
536
537 phylink_run_resolve(pl);
538}
539
Russell Kingce0aa272017-07-25 15:03:18 +0100540static const struct sfp_upstream_ops sfp_phylink_ops;
541
Russell King8fa7b9b2017-12-01 10:25:09 +0000542static int phylink_register_sfp(struct phylink *pl,
543 struct fwnode_handle *fwnode)
Russell Kingce0aa272017-07-25 15:03:18 +0100544{
Russell King8fa7b9b2017-12-01 10:25:09 +0000545 struct fwnode_reference_args ref;
546 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100547
Florian Fainelli02477802017-12-14 15:57:58 -0800548 if (!fwnode)
549 return 0;
550
Russell King8fa7b9b2017-12-01 10:25:09 +0000551 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
552 0, 0, &ref);
553 if (ret < 0) {
554 if (ret == -ENOENT)
555 return 0;
Russell Kingce0aa272017-07-25 15:03:18 +0100556
Ioana Ciornei170911802019-05-28 20:38:14 +0300557 phylink_err(pl, "unable to parse \"sfp\" node: %d\n",
558 ret);
Russell King8fa7b9b2017-12-01 10:25:09 +0000559 return ret;
560 }
561
562 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
Russell Kingce0aa272017-07-25 15:03:18 +0100563 &sfp_phylink_ops);
564 if (!pl->sfp_bus)
565 return -ENOMEM;
566
567 return 0;
568}
569
Russell King8796c892017-12-01 10:24:47 +0000570/**
571 * phylink_create() - create a phylink instance
572 * @ndev: a pointer to the &struct net_device
Russell King8fa7b9b2017-12-01 10:25:09 +0000573 * @fwnode: a pointer to a &struct fwnode_handle describing the network
574 * interface
Russell King8796c892017-12-01 10:24:47 +0000575 * @iface: the desired link mode defined by &typedef phy_interface_t
576 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
577 *
578 * Create a new phylink instance, and parse the link parameters found in @np.
579 * This will parse in-band modes, fixed-link or SFP configuration.
580 *
581 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
582 * must use IS_ERR() to check for errors from this function.
583 */
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300584struct phylink *phylink_create(struct phylink_config *config,
Russell King8fa7b9b2017-12-01 10:25:09 +0000585 struct fwnode_handle *fwnode,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700586 phy_interface_t iface,
587 const struct phylink_mac_ops *ops)
Russell King9525ae82017-07-25 15:03:13 +0100588{
589 struct phylink *pl;
590 int ret;
591
592 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
593 if (!pl)
594 return ERR_PTR(-ENOMEM);
595
596 mutex_init(&pl->state_mutex);
597 INIT_WORK(&pl->resolve, phylink_resolve);
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300598
599 pl->config = config;
600 if (config->type == PHYLINK_NETDEV) {
601 pl->netdev = to_net_dev(config->dev);
Ioana Ciornei43de6192019-05-28 20:38:13 +0300602 } else if (config->type == PHYLINK_DEV) {
603 pl->dev = config->dev;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300604 } else {
605 kfree(pl);
606 return ERR_PTR(-EINVAL);
607 }
608
Russell King9525ae82017-07-25 15:03:13 +0100609 pl->phy_state.interface = iface;
610 pl->link_interface = iface;
Florian Fainelli4be11ef2017-12-12 16:00:29 -0800611 if (iface == PHY_INTERFACE_MODE_MOCA)
612 pl->link_port = PORT_BNC;
613 else
614 pl->link_port = PORT_MII;
Russell King9525ae82017-07-25 15:03:13 +0100615 pl->link_config.interface = iface;
616 pl->link_config.pause = MLO_PAUSE_AN;
617 pl->link_config.speed = SPEED_UNKNOWN;
618 pl->link_config.duplex = DUPLEX_UNKNOWN;
Russell King74ee0e82017-12-20 23:21:34 +0000619 pl->link_config.an_enabled = true;
Russell King9525ae82017-07-25 15:03:13 +0100620 pl->ops = ops;
621 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King9cd00a82018-05-10 13:17:31 -0700622 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
Russell King9525ae82017-07-25 15:03:13 +0100623
624 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
625 linkmode_copy(pl->link_config.advertising, pl->supported);
626 phylink_validate(pl, pl->supported, &pl->link_config);
627
Russell King8fa7b9b2017-12-01 10:25:09 +0000628 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100629 if (ret < 0) {
630 kfree(pl);
631 return ERR_PTR(ret);
632 }
633
634 if (pl->link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000635 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100636 if (ret < 0) {
637 kfree(pl);
638 return ERR_PTR(ret);
639 }
640 }
641
Russell King8fa7b9b2017-12-01 10:25:09 +0000642 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100643 if (ret < 0) {
644 kfree(pl);
645 return ERR_PTR(ret);
646 }
647
Russell King9525ae82017-07-25 15:03:13 +0100648 return pl;
649}
650EXPORT_SYMBOL_GPL(phylink_create);
651
Russell King8796c892017-12-01 10:24:47 +0000652/**
653 * phylink_destroy() - cleanup and destroy the phylink instance
654 * @pl: a pointer to a &struct phylink returned from phylink_create()
655 *
656 * Destroy a phylink instance. Any PHY that has been attached must have been
657 * cleaned up via phylink_disconnect_phy() prior to calling this function.
658 */
Russell King9525ae82017-07-25 15:03:13 +0100659void phylink_destroy(struct phylink *pl)
660{
Russell Kingce0aa272017-07-25 15:03:18 +0100661 if (pl->sfp_bus)
662 sfp_unregister_upstream(pl->sfp_bus);
Florian Fainelli3bcd4772018-05-20 20:49:47 -0700663 if (!IS_ERR_OR_NULL(pl->link_gpio))
Florian Fainellidaab3342018-05-10 13:17:30 -0700664 gpiod_put(pl->link_gpio);
Russell Kingce0aa272017-07-25 15:03:18 +0100665
Russell King9525ae82017-07-25 15:03:13 +0100666 cancel_work_sync(&pl->resolve);
667 kfree(pl);
668}
669EXPORT_SYMBOL_GPL(phylink_destroy);
670
Wei Yongjun1ec6e532017-11-02 11:14:48 +0000671static void phylink_phy_change(struct phy_device *phydev, bool up,
672 bool do_carrier)
Russell King9525ae82017-07-25 15:03:13 +0100673{
674 struct phylink *pl = phydev->phylink;
675
676 mutex_lock(&pl->state_mutex);
677 pl->phy_state.speed = phydev->speed;
678 pl->phy_state.duplex = phydev->duplex;
679 pl->phy_state.pause = MLO_PAUSE_NONE;
680 if (phydev->pause)
681 pl->phy_state.pause |= MLO_PAUSE_SYM;
682 if (phydev->asym_pause)
683 pl->phy_state.pause |= MLO_PAUSE_ASYM;
684 pl->phy_state.interface = phydev->interface;
685 pl->phy_state.link = up;
686 mutex_unlock(&pl->state_mutex);
687
688 phylink_run_resolve(pl);
689
Ioana Ciornei170911802019-05-28 20:38:14 +0300690 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
691 phy_modes(phydev->interface),
692 phy_speed_to_str(phydev->speed),
693 phy_duplex_to_str(phydev->duplex));
Russell King9525ae82017-07-25 15:03:13 +0100694}
695
696static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
697{
698 struct phylink_link_state config;
699 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
Russell King9525ae82017-07-25 15:03:13 +0100700 int ret;
701
702 memset(&config, 0, sizeof(config));
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100703 linkmode_copy(supported, phy->supported);
704 linkmode_copy(config.advertising, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100705 config.interface = pl->link_config.interface;
706
707 /*
708 * This is the new way of dealing with flow control for PHYs,
709 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
710 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
711 * using our validate call to the MAC, we rely upon the MAC
712 * clearing the bits from both supported and advertising fields.
713 */
714 if (phylink_test(supported, Pause))
715 phylink_set(config.advertising, Pause);
716 if (phylink_test(supported, Asym_Pause))
717 phylink_set(config.advertising, Asym_Pause);
718
719 ret = phylink_validate(pl, supported, &config);
720 if (ret)
721 return ret;
722
723 phy->phylink = pl;
724 phy->phy_link_change = phylink_phy_change;
725
Ioana Ciornei170911802019-05-28 20:38:14 +0300726 phylink_info(pl,
727 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
728 phy->drv->name);
Russell King9525ae82017-07-25 15:03:13 +0100729
730 mutex_lock(&phy->lock);
731 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100732 pl->phydev = phy;
733 linkmode_copy(pl->supported, supported);
734 linkmode_copy(pl->link_config.advertising, config.advertising);
735
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000736 /* Restrict the phy advertisement according to the MAC support. */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100737 linkmode_copy(phy->advertising, config.advertising);
Russell King9525ae82017-07-25 15:03:13 +0100738 mutex_unlock(&pl->state_mutex);
739 mutex_unlock(&phy->lock);
740
Ioana Ciornei170911802019-05-28 20:38:14 +0300741 phylink_dbg(pl,
742 "phy: setting supported %*pb advertising %*pb\n",
743 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
744 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100745
Heiner Kallweit434a4312019-01-23 07:31:58 +0100746 if (phy_interrupt_is_valid(phy))
747 phy_request_interrupt(phy);
Russell King9525ae82017-07-25 15:03:13 +0100748
749 return 0;
750}
751
Baruch Siach7e418372018-10-03 19:04:49 +0300752static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
753 phy_interface_t interface)
754{
755 int ret;
756
757 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
758 (pl->link_an_mode == MLO_AN_INBAND &&
759 phy_interface_mode_is_8023z(interface))))
760 return -EINVAL;
761
762 if (pl->phydev)
763 return -EBUSY;
764
765 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
766 if (ret)
767 return ret;
768
769 ret = phylink_bringup_phy(pl, phy);
770 if (ret)
771 phy_detach(phy);
772
773 return ret;
774}
775
Russell King8796c892017-12-01 10:24:47 +0000776/**
777 * phylink_connect_phy() - connect a PHY to the phylink instance
778 * @pl: a pointer to a &struct phylink returned from phylink_create()
779 * @phy: a pointer to a &struct phy_device.
780 *
781 * Connect @phy to the phylink instance specified by @pl by calling
782 * phy_attach_direct(). Configure the @phy according to the MAC driver's
783 * capabilities, start the PHYLIB state machine and enable any interrupts
784 * that the PHY supports.
785 *
786 * This updates the phylink's ethtool supported and advertising link mode
787 * masks.
788 *
789 * Returns 0 on success or a negative errno.
790 */
Russell King9525ae82017-07-25 15:03:13 +0100791int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
792{
Florian Fainelli4904b6e2017-12-12 16:00:26 -0800793 /* Use PHY device/driver interface */
794 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
795 pl->link_interface = phy->interface;
796 pl->link_config.interface = pl->link_interface;
797 }
798
Baruch Siach7e418372018-10-03 19:04:49 +0300799 return __phylink_connect_phy(pl, phy, pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100800}
801EXPORT_SYMBOL_GPL(phylink_connect_phy);
802
Russell King8796c892017-12-01 10:24:47 +0000803/**
804 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
805 * @pl: a pointer to a &struct phylink returned from phylink_create()
806 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -0800807 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +0000808 *
809 * Connect the phy specified in the device node @dn to the phylink instance
810 * specified by @pl. Actions specified in phylink_connect_phy() will be
811 * performed.
812 *
813 * Returns 0 on success or a negative errno.
814 */
Florian Fainelli0a629642017-12-12 16:00:25 -0800815int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
816 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +0100817{
818 struct device_node *phy_node;
819 struct phy_device *phy_dev;
820 int ret;
821
Russell Kingcf4f2672017-12-01 10:24:21 +0000822 /* Fixed links and 802.3z are handled without needing a PHY */
823 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000824 (pl->link_an_mode == MLO_AN_INBAND &&
825 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100826 return 0;
827
828 phy_node = of_parse_phandle(dn, "phy-handle", 0);
829 if (!phy_node)
830 phy_node = of_parse_phandle(dn, "phy", 0);
831 if (!phy_node)
832 phy_node = of_parse_phandle(dn, "phy-device", 0);
833
834 if (!phy_node) {
Florian Fainellid38b4afd2017-12-12 16:00:27 -0800835 if (pl->link_an_mode == MLO_AN_PHY)
Russell King9525ae82017-07-25 15:03:13 +0100836 return -ENODEV;
Russell King9525ae82017-07-25 15:03:13 +0100837 return 0;
838 }
839
Florian Fainelli0a629642017-12-12 16:00:25 -0800840 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
841 pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100842 /* We're done with the phy_node handle */
843 of_node_put(phy_node);
844
845 if (!phy_dev)
846 return -ENODEV;
847
848 ret = phylink_bringup_phy(pl, phy_dev);
849 if (ret)
850 phy_detach(phy_dev);
851
852 return ret;
853}
854EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
855
Russell King8796c892017-12-01 10:24:47 +0000856/**
857 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
858 * instance.
859 * @pl: a pointer to a &struct phylink returned from phylink_create()
860 *
861 * Disconnect any current PHY from the phylink instance described by @pl.
862 */
Russell King9525ae82017-07-25 15:03:13 +0100863void phylink_disconnect_phy(struct phylink *pl)
864{
865 struct phy_device *phy;
866
Russell King8b874512017-12-15 16:09:47 +0000867 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100868
869 phy = pl->phydev;
870 if (phy) {
871 mutex_lock(&phy->lock);
872 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100873 pl->phydev = NULL;
874 mutex_unlock(&pl->state_mutex);
875 mutex_unlock(&phy->lock);
876 flush_work(&pl->resolve);
877
878 phy_disconnect(phy);
879 }
880}
881EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
882
Russell King8796c892017-12-01 10:24:47 +0000883/**
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800884 * phylink_fixed_state_cb() - allow setting a fixed link callback
885 * @pl: a pointer to a &struct phylink returned from phylink_create()
886 * @cb: callback to execute to determine the fixed link state.
887 *
888 * The MAC driver should call this driver when the state of its link
889 * can be determined through e.g: an out of band MMIO register.
890 */
891int phylink_fixed_state_cb(struct phylink *pl,
892 void (*cb)(struct net_device *dev,
893 struct phylink_link_state *state))
894{
895 /* It does not make sense to let the link be overriden unless we use
896 * MLO_AN_FIXED
897 */
898 if (pl->link_an_mode != MLO_AN_FIXED)
899 return -EINVAL;
900
901 mutex_lock(&pl->state_mutex);
902 pl->get_fixed_state = cb;
903 mutex_unlock(&pl->state_mutex);
904
905 return 0;
906}
907EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
908
909/**
Russell King8796c892017-12-01 10:24:47 +0000910 * phylink_mac_change() - notify phylink of a change in MAC state
911 * @pl: a pointer to a &struct phylink returned from phylink_create()
912 * @up: indicates whether the link is currently up.
913 *
914 * The MAC driver should call this driver when the state of its link
915 * changes (eg, link failure, new negotiation results, etc.)
916 */
Russell King9525ae82017-07-25 15:03:13 +0100917void phylink_mac_change(struct phylink *pl, bool up)
918{
919 if (!up)
920 pl->mac_link_dropped = true;
921 phylink_run_resolve(pl);
Ioana Ciornei170911802019-05-28 20:38:14 +0300922 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
Russell King9525ae82017-07-25 15:03:13 +0100923}
924EXPORT_SYMBOL_GPL(phylink_mac_change);
925
Russell King8796c892017-12-01 10:24:47 +0000926/**
927 * phylink_start() - start a phylink instance
928 * @pl: a pointer to a &struct phylink returned from phylink_create()
929 *
930 * Start the phylink instance specified by @pl, configuring the MAC for the
931 * desired link mode(s) and negotiation style. This should be called from the
932 * network device driver's &struct net_device_ops ndo_open() method.
933 */
Russell King9525ae82017-07-25 15:03:13 +0100934void phylink_start(struct phylink *pl)
935{
Russell King8b874512017-12-15 16:09:47 +0000936 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100937
Ioana Ciornei170911802019-05-28 20:38:14 +0300938 phylink_info(pl, "configuring for %s/%s link mode\n",
939 phylink_an_mode_str(pl->link_an_mode),
940 phy_modes(pl->link_config.interface));
Russell King9525ae82017-07-25 15:03:13 +0100941
Antoine Tenartaeeb2e82018-09-19 11:39:31 +0200942 /* Always set the carrier off */
Ioana Ciornei43de6192019-05-28 20:38:13 +0300943 if (pl->netdev)
944 netif_carrier_off(pl->netdev);
Antoine Tenartaeeb2e82018-09-19 11:39:31 +0200945
Russell King9525ae82017-07-25 15:03:13 +0100946 /* Apply the link configuration to the MAC when starting. This allows
947 * a fixed-link to start with the correct parameters, and also
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000948 * ensures that we set the appropriate advertisement for Serdes links.
Russell King9525ae82017-07-25 15:03:13 +0100949 */
950 phylink_resolve_flow(pl, &pl->link_config);
951 phylink_mac_config(pl, &pl->link_config);
952
Russell King85b43942017-12-01 10:24:42 +0000953 /* Restart autonegotiation if using 802.3z to ensure that the link
954 * parameters are properly negotiated. This is necessary for DSA
955 * switches using 802.3z negotiation to ensure they see our modes.
956 */
957 phylink_mac_an_restart(pl);
958
Russell King9525ae82017-07-25 15:03:13 +0100959 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
960 phylink_run_resolve(pl);
961
Russell King9cd00a82018-05-10 13:17:31 -0700962 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
963 mod_timer(&pl->link_poll, jiffies + HZ);
Russell Kingce0aa272017-07-25 15:03:18 +0100964 if (pl->sfp_bus)
965 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100966 if (pl->phydev)
967 phy_start(pl->phydev);
968}
969EXPORT_SYMBOL_GPL(phylink_start);
970
Russell King8796c892017-12-01 10:24:47 +0000971/**
972 * phylink_stop() - stop a phylink instance
973 * @pl: a pointer to a &struct phylink returned from phylink_create()
974 *
975 * Stop the phylink instance specified by @pl. This should be called from the
976 * network device driver's &struct net_device_ops ndo_stop() method. The
977 * network device's carrier state should not be changed prior to calling this
978 * function.
979 */
Russell King9525ae82017-07-25 15:03:13 +0100980void phylink_stop(struct phylink *pl)
981{
Russell King8b874512017-12-15 16:09:47 +0000982 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100983
984 if (pl->phydev)
985 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100986 if (pl->sfp_bus)
987 sfp_upstream_stop(pl->sfp_bus);
Russell King9cd00a82018-05-10 13:17:31 -0700988 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
989 del_timer_sync(&pl->link_poll);
Russell King9525ae82017-07-25 15:03:13 +0100990
Russell King87454b62019-02-11 15:04:24 +0000991 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
Russell King9525ae82017-07-25 15:03:13 +0100992}
993EXPORT_SYMBOL_GPL(phylink_stop);
994
Russell King8796c892017-12-01 10:24:47 +0000995/**
996 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
997 * @pl: a pointer to a &struct phylink returned from phylink_create()
998 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
999 *
1000 * Read the wake on lan parameters from the PHY attached to the phylink
1001 * instance specified by @pl. If no PHY is currently attached, report no
1002 * support for wake on lan.
1003 */
Russell King9525ae82017-07-25 15:03:13 +01001004void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1005{
Russell King8b874512017-12-15 16:09:47 +00001006 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001007
1008 wol->supported = 0;
1009 wol->wolopts = 0;
1010
1011 if (pl->phydev)
1012 phy_ethtool_get_wol(pl->phydev, wol);
1013}
1014EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1015
Russell King8796c892017-12-01 10:24:47 +00001016/**
1017 * phylink_ethtool_set_wol() - set wake on lan parameters
1018 * @pl: a pointer to a &struct phylink returned from phylink_create()
1019 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1020 *
1021 * Set the wake on lan parameters for the PHY attached to the phylink
1022 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1023 * error.
1024 *
1025 * Returns zero on success or negative errno code.
1026 */
Russell King9525ae82017-07-25 15:03:13 +01001027int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1028{
1029 int ret = -EOPNOTSUPP;
1030
Russell King8b874512017-12-15 16:09:47 +00001031 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001032
1033 if (pl->phydev)
1034 ret = phy_ethtool_set_wol(pl->phydev, wol);
1035
1036 return ret;
1037}
1038EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1039
1040static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1041{
1042 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1043
1044 linkmode_zero(mask);
1045 phylink_set_port_modes(mask);
1046
1047 linkmode_and(dst, dst, mask);
1048 linkmode_or(dst, dst, b);
1049}
1050
1051static void phylink_get_ksettings(const struct phylink_link_state *state,
1052 struct ethtool_link_ksettings *kset)
1053{
1054 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1055 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1056 kset->base.speed = state->speed;
1057 kset->base.duplex = state->duplex;
1058 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1059 AUTONEG_DISABLE;
1060}
1061
Russell King8796c892017-12-01 10:24:47 +00001062/**
1063 * phylink_ethtool_ksettings_get() - get the current link settings
1064 * @pl: a pointer to a &struct phylink returned from phylink_create()
1065 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1066 *
1067 * Read the current link settings for the phylink instance specified by @pl.
1068 * This will be the link settings read from the MAC, PHY or fixed link
1069 * settings depending on the current negotiation mode.
1070 */
Russell King9525ae82017-07-25 15:03:13 +01001071int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001072 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001073{
1074 struct phylink_link_state link_state;
1075
Russell King8b874512017-12-15 16:09:47 +00001076 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001077
1078 if (pl->phydev) {
1079 phy_ethtool_ksettings_get(pl->phydev, kset);
1080 } else {
1081 kset->base.port = pl->link_port;
1082 }
1083
1084 linkmode_copy(kset->link_modes.supported, pl->supported);
1085
1086 switch (pl->link_an_mode) {
1087 case MLO_AN_FIXED:
1088 /* We are using fixed settings. Report these as the
1089 * current link settings - and note that these also
1090 * represent the supported speeds/duplex/pause modes.
1091 */
1092 phylink_get_fixed_state(pl, &link_state);
1093 phylink_get_ksettings(&link_state, kset);
1094 break;
1095
Russell King86a362c2017-12-01 10:24:26 +00001096 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001097 /* If there is a phy attached, then use the reported
1098 * settings from the phy with no modification.
1099 */
1100 if (pl->phydev)
1101 break;
1102
Russell King9525ae82017-07-25 15:03:13 +01001103 phylink_get_mac_state(pl, &link_state);
1104
1105 /* The MAC is reporting the link results from its own PCS
1106 * layer via in-band status. Report these as the current
1107 * link settings.
1108 */
1109 phylink_get_ksettings(&link_state, kset);
1110 break;
1111 }
1112
1113 return 0;
1114}
1115EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1116
Russell King8796c892017-12-01 10:24:47 +00001117/**
1118 * phylink_ethtool_ksettings_set() - set the link settings
1119 * @pl: a pointer to a &struct phylink returned from phylink_create()
1120 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1121 */
Russell King9525ae82017-07-25 15:03:13 +01001122int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001123 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001124{
1125 struct ethtool_link_ksettings our_kset;
1126 struct phylink_link_state config;
1127 int ret;
1128
Russell King8b874512017-12-15 16:09:47 +00001129 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001130
1131 if (kset->base.autoneg != AUTONEG_DISABLE &&
1132 kset->base.autoneg != AUTONEG_ENABLE)
1133 return -EINVAL;
1134
1135 config = pl->link_config;
1136
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001137 /* Mask out unsupported advertisements */
Russell King9525ae82017-07-25 15:03:13 +01001138 linkmode_and(config.advertising, kset->link_modes.advertising,
1139 pl->supported);
1140
1141 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1142 if (kset->base.autoneg == AUTONEG_DISABLE) {
1143 const struct phy_setting *s;
1144
1145 /* Autonegotiation disabled, select a suitable speed and
1146 * duplex.
1147 */
1148 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001149 pl->supported, false);
Russell King9525ae82017-07-25 15:03:13 +01001150 if (!s)
1151 return -EINVAL;
1152
1153 /* If we have a fixed link (as specified by firmware), refuse
1154 * to change link parameters.
1155 */
1156 if (pl->link_an_mode == MLO_AN_FIXED &&
1157 (s->speed != pl->link_config.speed ||
1158 s->duplex != pl->link_config.duplex))
1159 return -EINVAL;
1160
1161 config.speed = s->speed;
1162 config.duplex = s->duplex;
1163 config.an_enabled = false;
1164
1165 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1166 } else {
1167 /* If we have a fixed link, refuse to enable autonegotiation */
1168 if (pl->link_an_mode == MLO_AN_FIXED)
1169 return -EINVAL;
1170
1171 config.speed = SPEED_UNKNOWN;
1172 config.duplex = DUPLEX_UNKNOWN;
1173 config.an_enabled = true;
1174
1175 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1176 }
1177
1178 if (phylink_validate(pl, pl->supported, &config))
1179 return -EINVAL;
1180
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001181 /* If autonegotiation is enabled, we must have an advertisement */
Russell King9525ae82017-07-25 15:03:13 +01001182 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1183 return -EINVAL;
1184
1185 our_kset = *kset;
1186 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1187 our_kset.base.speed = config.speed;
1188 our_kset.base.duplex = config.duplex;
1189
1190 /* If we have a PHY, configure the phy */
1191 if (pl->phydev) {
1192 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1193 if (ret)
1194 return ret;
1195 }
1196
1197 mutex_lock(&pl->state_mutex);
1198 /* Configure the MAC to match the new settings */
1199 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
Russell King182088a2017-12-20 23:21:28 +00001200 pl->link_config.interface = config.interface;
Russell King9525ae82017-07-25 15:03:13 +01001201 pl->link_config.speed = our_kset.base.speed;
1202 pl->link_config.duplex = our_kset.base.duplex;
1203 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1204
1205 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1206 phylink_mac_config(pl, &pl->link_config);
1207 phylink_mac_an_restart(pl);
1208 }
1209 mutex_unlock(&pl->state_mutex);
1210
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001211 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001212}
1213EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1214
Russell King8796c892017-12-01 10:24:47 +00001215/**
1216 * phylink_ethtool_nway_reset() - restart negotiation
1217 * @pl: a pointer to a &struct phylink returned from phylink_create()
1218 *
1219 * Restart negotiation for the phylink instance specified by @pl. This will
1220 * cause any attached phy to restart negotiation with the link partner, and
1221 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1222 * negotiation.
1223 *
1224 * Returns zero on success, or negative error code.
1225 */
Russell King9525ae82017-07-25 15:03:13 +01001226int phylink_ethtool_nway_reset(struct phylink *pl)
1227{
1228 int ret = 0;
1229
Russell King8b874512017-12-15 16:09:47 +00001230 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001231
1232 if (pl->phydev)
1233 ret = phy_restart_aneg(pl->phydev);
1234 phylink_mac_an_restart(pl);
1235
1236 return ret;
1237}
1238EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1239
Russell King8796c892017-12-01 10:24:47 +00001240/**
1241 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1242 * @pl: a pointer to a &struct phylink returned from phylink_create()
1243 * @pause: a pointer to a &struct ethtool_pauseparam
1244 */
Russell King9525ae82017-07-25 15:03:13 +01001245void phylink_ethtool_get_pauseparam(struct phylink *pl,
1246 struct ethtool_pauseparam *pause)
1247{
Russell King8b874512017-12-15 16:09:47 +00001248 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001249
1250 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1251 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1252 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1253}
1254EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1255
Russell King8796c892017-12-01 10:24:47 +00001256/**
1257 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1258 * @pl: a pointer to a &struct phylink returned from phylink_create()
1259 * @pause: a pointer to a &struct ethtool_pauseparam
1260 */
Russell King9525ae82017-07-25 15:03:13 +01001261int phylink_ethtool_set_pauseparam(struct phylink *pl,
1262 struct ethtool_pauseparam *pause)
1263{
1264 struct phylink_link_state *config = &pl->link_config;
1265
Russell King8b874512017-12-15 16:09:47 +00001266 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001267
1268 if (!phylink_test(pl->supported, Pause) &&
1269 !phylink_test(pl->supported, Asym_Pause))
1270 return -EOPNOTSUPP;
1271
1272 if (!phylink_test(pl->supported, Asym_Pause) &&
1273 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1274 return -EINVAL;
1275
1276 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1277
1278 if (pause->autoneg)
1279 config->pause |= MLO_PAUSE_AN;
1280 if (pause->rx_pause)
1281 config->pause |= MLO_PAUSE_RX;
1282 if (pause->tx_pause)
1283 config->pause |= MLO_PAUSE_TX;
1284
1285 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1286 switch (pl->link_an_mode) {
1287 case MLO_AN_PHY:
1288 /* Silently mark the carrier down, and then trigger a resolve */
Ioana Ciornei43de6192019-05-28 20:38:13 +03001289 if (pl->netdev)
1290 netif_carrier_off(pl->netdev);
Russell King9525ae82017-07-25 15:03:13 +01001291 phylink_run_resolve(pl);
1292 break;
1293
1294 case MLO_AN_FIXED:
1295 /* Should we allow fixed links to change against the config? */
1296 phylink_resolve_flow(pl, config);
1297 phylink_mac_config(pl, config);
1298 break;
1299
Russell King86a362c2017-12-01 10:24:26 +00001300 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001301 phylink_mac_config(pl, config);
1302 phylink_mac_an_restart(pl);
1303 break;
1304 }
1305 }
1306
1307 return 0;
1308}
1309EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1310
Russell King8796c892017-12-01 10:24:47 +00001311/**
1312 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1313 * counter
1314 * @pl: a pointer to a &struct phylink returned from phylink_create().
1315 *
1316 * Read the Energy Efficient Ethernet error counter from the PHY associated
1317 * with the phylink instance specified by @pl.
1318 *
1319 * Returns positive error counter value, or negative error code.
1320 */
Russell King9525ae82017-07-25 15:03:13 +01001321int phylink_get_eee_err(struct phylink *pl)
1322{
1323 int ret = 0;
1324
Russell King8b874512017-12-15 16:09:47 +00001325 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001326
1327 if (pl->phydev)
1328 ret = phy_get_eee_err(pl->phydev);
1329
1330 return ret;
1331}
1332EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1333
Russell King8796c892017-12-01 10:24:47 +00001334/**
Russell King86e58132019-02-11 11:46:06 +00001335 * phylink_init_eee() - init and check the EEE features
1336 * @pl: a pointer to a &struct phylink returned from phylink_create()
1337 * @clk_stop_enable: allow PHY to stop receive clock
1338 *
1339 * Must be called either with RTNL held or within mac_link_up()
1340 */
1341int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1342{
1343 int ret = -EOPNOTSUPP;
1344
1345 if (pl->phydev)
1346 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1347
1348 return ret;
1349}
1350EXPORT_SYMBOL_GPL(phylink_init_eee);
1351
1352/**
Russell King8796c892017-12-01 10:24:47 +00001353 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1354 * @pl: a pointer to a &struct phylink returned from phylink_create()
1355 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1356 */
Russell King9525ae82017-07-25 15:03:13 +01001357int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1358{
1359 int ret = -EOPNOTSUPP;
1360
Russell King8b874512017-12-15 16:09:47 +00001361 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001362
1363 if (pl->phydev)
1364 ret = phy_ethtool_get_eee(pl->phydev, eee);
1365
1366 return ret;
1367}
1368EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1369
Russell King8796c892017-12-01 10:24:47 +00001370/**
1371 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1372 * @pl: a pointer to a &struct phylink returned from phylink_create()
1373 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1374 */
Russell King9525ae82017-07-25 15:03:13 +01001375int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1376{
1377 int ret = -EOPNOTSUPP;
1378
Russell King8b874512017-12-15 16:09:47 +00001379 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001380
1381 if (pl->phydev)
1382 ret = phy_ethtool_set_eee(pl->phydev, eee);
1383
1384 return ret;
1385}
1386EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1387
1388/* This emulates MII registers for a fixed-mode phy operating as per the
1389 * passed in state. "aneg" defines if we report negotiation is possible.
1390 *
1391 * FIXME: should deal with negotiation state too.
1392 */
1393static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1394 struct phylink_link_state *state, bool aneg)
1395{
1396 struct fixed_phy_status fs;
1397 int val;
1398
1399 fs.link = state->link;
1400 fs.speed = state->speed;
1401 fs.duplex = state->duplex;
1402 fs.pause = state->pause & MLO_PAUSE_SYM;
1403 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1404
1405 val = swphy_read_reg(reg, &fs);
1406 if (reg == MII_BMSR) {
1407 if (!state->an_complete)
1408 val &= ~BMSR_ANEGCOMPLETE;
1409 if (!aneg)
1410 val &= ~BMSR_ANEGCAPABLE;
1411 }
1412 return val;
1413}
1414
Russell Kingecbd87b2017-07-25 15:03:28 +01001415static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1416 unsigned int reg)
1417{
1418 struct phy_device *phydev = pl->phydev;
1419 int prtad, devad;
1420
1421 if (mdio_phy_id_is_c45(phy_id)) {
1422 prtad = mdio_phy_id_prtad(phy_id);
1423 devad = mdio_phy_id_devad(phy_id);
1424 devad = MII_ADDR_C45 | devad << 16 | reg;
1425 } else if (phydev->is_c45) {
1426 switch (reg) {
1427 case MII_BMCR:
1428 case MII_BMSR:
1429 case MII_PHYSID1:
1430 case MII_PHYSID2:
1431 devad = __ffs(phydev->c45_ids.devices_in_package);
1432 break;
1433 case MII_ADVERTISE:
1434 case MII_LPA:
1435 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1436 return -EINVAL;
1437 devad = MDIO_MMD_AN;
1438 if (reg == MII_ADVERTISE)
1439 reg = MDIO_AN_ADVERTISE;
1440 else
1441 reg = MDIO_AN_LPA;
1442 break;
1443 default:
1444 return -EINVAL;
1445 }
1446 prtad = phy_id;
1447 devad = MII_ADDR_C45 | devad << 16 | reg;
1448 } else {
1449 prtad = phy_id;
1450 devad = reg;
1451 }
1452 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1453}
1454
1455static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1456 unsigned int reg, unsigned int val)
1457{
1458 struct phy_device *phydev = pl->phydev;
1459 int prtad, devad;
1460
1461 if (mdio_phy_id_is_c45(phy_id)) {
1462 prtad = mdio_phy_id_prtad(phy_id);
1463 devad = mdio_phy_id_devad(phy_id);
1464 devad = MII_ADDR_C45 | devad << 16 | reg;
1465 } else if (phydev->is_c45) {
1466 switch (reg) {
1467 case MII_BMCR:
1468 case MII_BMSR:
1469 case MII_PHYSID1:
1470 case MII_PHYSID2:
1471 devad = __ffs(phydev->c45_ids.devices_in_package);
1472 break;
1473 case MII_ADVERTISE:
1474 case MII_LPA:
1475 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1476 return -EINVAL;
1477 devad = MDIO_MMD_AN;
1478 if (reg == MII_ADVERTISE)
1479 reg = MDIO_AN_ADVERTISE;
1480 else
1481 reg = MDIO_AN_LPA;
1482 break;
1483 default:
1484 return -EINVAL;
1485 }
1486 prtad = phy_id;
1487 devad = MII_ADDR_C45 | devad << 16 | reg;
1488 } else {
1489 prtad = phy_id;
1490 devad = reg;
1491 }
1492
1493 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1494}
1495
Russell King9525ae82017-07-25 15:03:13 +01001496static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1497 unsigned int reg)
1498{
1499 struct phylink_link_state state;
1500 int val = 0xffff;
1501
Russell King9525ae82017-07-25 15:03:13 +01001502 switch (pl->link_an_mode) {
1503 case MLO_AN_FIXED:
1504 if (phy_id == 0) {
1505 phylink_get_fixed_state(pl, &state);
1506 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1507 true);
1508 }
1509 break;
1510
1511 case MLO_AN_PHY:
1512 return -EOPNOTSUPP;
1513
Russell King86a362c2017-12-01 10:24:26 +00001514 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001515 if (phy_id == 0) {
1516 val = phylink_get_mac_state(pl, &state);
1517 if (val < 0)
1518 return val;
1519
1520 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1521 true);
1522 }
1523 break;
1524 }
1525
1526 return val & 0xffff;
1527}
1528
1529static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1530 unsigned int reg, unsigned int val)
1531{
Russell King9525ae82017-07-25 15:03:13 +01001532 switch (pl->link_an_mode) {
1533 case MLO_AN_FIXED:
1534 break;
1535
1536 case MLO_AN_PHY:
1537 return -EOPNOTSUPP;
1538
Russell King86a362c2017-12-01 10:24:26 +00001539 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001540 break;
1541 }
1542
1543 return 0;
1544}
1545
Russell King8796c892017-12-01 10:24:47 +00001546/**
1547 * phylink_mii_ioctl() - generic mii ioctl interface
1548 * @pl: a pointer to a &struct phylink returned from phylink_create()
1549 * @ifr: a pointer to a &struct ifreq for socket ioctls
1550 * @cmd: ioctl cmd to execute
1551 *
1552 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1553 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1554 *
1555 * Returns: zero on success or negative error code.
1556 *
1557 * %SIOCGMIIPHY:
1558 * read register from the current PHY.
1559 * %SIOCGMIIREG:
1560 * read register from the specified PHY.
1561 * %SIOCSMIIREG:
1562 * set a register on the specified PHY.
1563 */
Russell King9525ae82017-07-25 15:03:13 +01001564int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1565{
Russell Kingecbd87b2017-07-25 15:03:28 +01001566 struct mii_ioctl_data *mii = if_mii(ifr);
1567 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001568
Russell King8b874512017-12-15 16:09:47 +00001569 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001570
Russell Kingecbd87b2017-07-25 15:03:28 +01001571 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001572 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001573 switch (cmd) {
1574 case SIOCGMIIPHY:
1575 mii->phy_id = pl->phydev->mdio.addr;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001576 /* fall through */
Russell King9525ae82017-07-25 15:03:13 +01001577
Russell Kingecbd87b2017-07-25 15:03:28 +01001578 case SIOCGMIIREG:
1579 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1580 if (ret >= 0) {
1581 mii->val_out = ret;
1582 ret = 0;
1583 }
1584 break;
Russell King9525ae82017-07-25 15:03:13 +01001585
Russell Kingecbd87b2017-07-25 15:03:28 +01001586 case SIOCSMIIREG:
1587 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1588 mii->val_in);
1589 break;
Russell King9525ae82017-07-25 15:03:13 +01001590
Russell Kingecbd87b2017-07-25 15:03:28 +01001591 default:
Russell King9525ae82017-07-25 15:03:13 +01001592 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001593 break;
1594 }
1595 } else {
1596 switch (cmd) {
1597 case SIOCGMIIPHY:
1598 mii->phy_id = 0;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001599 /* fall through */
Russell Kingecbd87b2017-07-25 15:03:28 +01001600
1601 case SIOCGMIIREG:
1602 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1603 if (ret >= 0) {
1604 mii->val_out = ret;
1605 ret = 0;
1606 }
1607 break;
1608
1609 case SIOCSMIIREG:
1610 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1611 mii->val_in);
1612 break;
1613
1614 default:
1615 ret = -EOPNOTSUPP;
1616 break;
1617 }
Russell King9525ae82017-07-25 15:03:13 +01001618 }
1619
1620 return ret;
1621}
1622EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1623
Russell Kingce0aa272017-07-25 15:03:18 +01001624static int phylink_sfp_module_insert(void *upstream,
1625 const struct sfp_eeprom_id *id)
1626{
1627 struct phylink *pl = upstream;
1628 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1629 struct phylink_link_state config;
1630 phy_interface_t iface;
Russell King444d3502017-12-29 12:15:33 +00001631 int ret = 0;
Russell Kingce0aa272017-07-25 15:03:18 +01001632 bool changed;
1633 u8 port;
1634
Russell King8b874512017-12-15 16:09:47 +00001635 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001636
Russell Kinga9c79362018-02-27 15:53:02 +00001637 sfp_parse_support(pl->sfp_bus, id, support);
1638 port = sfp_parse_port(pl->sfp_bus, id, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001639
1640 memset(&config, 0, sizeof(config));
1641 linkmode_copy(config.advertising, support);
Russell Kinga9c79362018-02-27 15:53:02 +00001642 config.interface = PHY_INTERFACE_MODE_NA;
Russell Kingce0aa272017-07-25 15:03:18 +01001643 config.speed = SPEED_UNKNOWN;
1644 config.duplex = DUPLEX_UNKNOWN;
1645 config.pause = MLO_PAUSE_AN;
1646 config.an_enabled = pl->link_config.an_enabled;
1647
1648 /* Ignore errors if we're expecting a PHY to attach later */
1649 ret = phylink_validate(pl, support, &config);
1650 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001651 phylink_err(pl, "validation with support %*pb failed: %d\n",
1652 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kinga9c79362018-02-27 15:53:02 +00001653 return ret;
1654 }
1655
1656 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1657 if (iface == PHY_INTERFACE_MODE_NA) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001658 phylink_err(pl,
1659 "selection of interface failed, advertisement %*pb\n",
1660 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
Russell Kinga9c79362018-02-27 15:53:02 +00001661 return -EINVAL;
1662 }
1663
1664 config.interface = iface;
1665 ret = phylink_validate(pl, support, &config);
1666 if (ret) {
Ioana Ciornei170911802019-05-28 20:38:14 +03001667 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
1668 phylink_an_mode_str(MLO_AN_INBAND),
1669 phy_modes(config.interface),
1670 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
Russell Kingce0aa272017-07-25 15:03:18 +01001671 return ret;
1672 }
1673
Ioana Ciornei170911802019-05-28 20:38:14 +03001674 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
1675 phylink_an_mode_str(MLO_AN_INBAND),
1676 phy_modes(config.interface),
1677 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001678
Russell King86a362c2017-12-01 10:24:26 +00001679 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001680 return -EINVAL;
1681
1682 changed = !bitmap_equal(pl->supported, support,
1683 __ETHTOOL_LINK_MODE_MASK_NBITS);
1684 if (changed) {
1685 linkmode_copy(pl->supported, support);
1686 linkmode_copy(pl->link_config.advertising, config.advertising);
1687 }
1688
Russell King444d3502017-12-29 12:15:33 +00001689 if (pl->link_an_mode != MLO_AN_INBAND ||
Russell Kingce0aa272017-07-25 15:03:18 +01001690 pl->link_config.interface != config.interface) {
1691 pl->link_config.interface = config.interface;
Russell King444d3502017-12-29 12:15:33 +00001692 pl->link_an_mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001693
1694 changed = true;
1695
Ioana Ciornei170911802019-05-28 20:38:14 +03001696 phylink_info(pl, "switched to %s/%s link mode\n",
1697 phylink_an_mode_str(MLO_AN_INBAND),
1698 phy_modes(config.interface));
Russell Kingce0aa272017-07-25 15:03:18 +01001699 }
1700
1701 pl->link_port = port;
1702
1703 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1704 &pl->phylink_disable_state))
1705 phylink_mac_config(pl, &pl->link_config);
1706
1707 return ret;
1708}
1709
1710static void phylink_sfp_link_down(void *upstream)
1711{
1712 struct phylink *pl = upstream;
1713
Russell King8b874512017-12-15 16:09:47 +00001714 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001715
Russell King87454b62019-02-11 15:04:24 +00001716 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
Russell Kingce0aa272017-07-25 15:03:18 +01001717}
1718
1719static void phylink_sfp_link_up(void *upstream)
1720{
1721 struct phylink *pl = upstream;
1722
Russell King8b874512017-12-15 16:09:47 +00001723 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001724
1725 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1726 phylink_run_resolve(pl);
1727}
1728
1729static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1730{
Baruch Siach7e418372018-10-03 19:04:49 +03001731 struct phylink *pl = upstream;
1732
1733 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
Russell Kingce0aa272017-07-25 15:03:18 +01001734}
1735
1736static void phylink_sfp_disconnect_phy(void *upstream)
1737{
1738 phylink_disconnect_phy(upstream);
1739}
1740
1741static const struct sfp_upstream_ops sfp_phylink_ops = {
1742 .module_insert = phylink_sfp_module_insert,
1743 .link_up = phylink_sfp_link_up,
1744 .link_down = phylink_sfp_link_down,
1745 .connect_phy = phylink_sfp_connect_phy,
1746 .disconnect_phy = phylink_sfp_disconnect_phy,
1747};
1748
Russell King624c0f02018-08-09 15:38:38 +02001749/* Helpers for MAC drivers */
1750
1751/**
1752 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1753 * @state: a pointer to a &struct phylink_link_state
1754 *
1755 * Inspect the interface mode, advertising mask or forced speed and
1756 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1757 * the interface mode to suit. @state->interface is appropriately
1758 * updated, and the advertising mask has the "other" baseX_Full flag
1759 * cleared.
1760 */
1761void phylink_helper_basex_speed(struct phylink_link_state *state)
1762{
1763 if (phy_interface_mode_is_8023z(state->interface)) {
1764 bool want_2500 = state->an_enabled ?
1765 phylink_test(state->advertising, 2500baseX_Full) :
1766 state->speed == SPEED_2500;
1767
1768 if (want_2500) {
1769 phylink_clear(state->advertising, 1000baseX_Full);
1770 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1771 } else {
1772 phylink_clear(state->advertising, 2500baseX_Full);
1773 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1774 }
1775 }
1776}
1777EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1778
Andrew Lunn5f857572019-01-21 19:10:19 +01001779MODULE_LICENSE("GPL v2");