blob: 74983593834b04a39e8272a899873a9a8626a633 [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;
44
45 unsigned long phylink_disable_state; /* bitmask of disables */
46 struct phy_device *phydev;
47 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
48 u8 link_an_mode; /* MLO_AN_xxx */
49 u8 link_port; /* The current non-phy ethtool port */
50 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
51
52 /* The link configuration settings */
53 struct phylink_link_state link_config;
54 struct gpio_desc *link_gpio;
Russell King9cd00a82018-05-10 13:17:31 -070055 struct timer_list link_poll;
Florian Fainelli1ac63e32017-12-12 16:00:28 -080056 void (*get_fixed_state)(struct net_device *dev,
57 struct phylink_link_state *s);
Russell King9525ae82017-07-25 15:03:13 +010058
59 struct mutex state_mutex;
60 struct phylink_link_state phy_state;
61 struct work_struct resolve;
62
63 bool mac_link_dropped;
Russell Kingce0aa272017-07-25 15:03:18 +010064
65 struct sfp_bus *sfp_bus;
Russell King9525ae82017-07-25 15:03:13 +010066};
67
Russell King8796c892017-12-01 10:24:47 +000068/**
69 * phylink_set_port_modes() - set the port type modes in the ethtool mask
70 * @mask: ethtool link mode mask
71 *
72 * Sets all the port type modes in the ethtool mask. MAC drivers should
73 * use this in their 'validate' callback.
74 */
Russell King9525ae82017-07-25 15:03:13 +010075void phylink_set_port_modes(unsigned long *mask)
76{
77 phylink_set(mask, TP);
78 phylink_set(mask, AUI);
79 phylink_set(mask, MII);
80 phylink_set(mask, FIBRE);
81 phylink_set(mask, BNC);
82 phylink_set(mask, Backplane);
83}
84EXPORT_SYMBOL_GPL(phylink_set_port_modes);
85
86static int phylink_is_empty_linkmode(const unsigned long *linkmode)
87{
88 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
89
90 phylink_set_port_modes(tmp);
91 phylink_set(tmp, Autoneg);
92 phylink_set(tmp, Pause);
93 phylink_set(tmp, Asym_Pause);
94
95 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
96
97 return linkmode_empty(tmp);
98}
99
100static const char *phylink_an_mode_str(unsigned int mode)
101{
102 static const char *modestr[] = {
103 [MLO_AN_PHY] = "phy",
104 [MLO_AN_FIXED] = "fixed",
Russell King86a362c2017-12-01 10:24:26 +0000105 [MLO_AN_INBAND] = "inband",
Russell King9525ae82017-07-25 15:03:13 +0100106 };
107
108 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
109}
110
111static int phylink_validate(struct phylink *pl, unsigned long *supported,
112 struct phylink_link_state *state)
113{
114 pl->ops->validate(pl->netdev, supported, state);
115
116 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
117}
118
Russell King8fa7b9b2017-12-01 10:25:09 +0000119static int phylink_parse_fixedlink(struct phylink *pl,
120 struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100121{
Russell King8fa7b9b2017-12-01 10:25:09 +0000122 struct fwnode_handle *fixed_node;
Russell King9525ae82017-07-25 15:03:13 +0100123 const struct phy_setting *s;
124 struct gpio_desc *desc;
Russell King9525ae82017-07-25 15:03:13 +0100125 u32 speed;
Russell King8fa7b9b2017-12-01 10:25:09 +0000126 int ret;
Russell King9525ae82017-07-25 15:03:13 +0100127
Russell King8fa7b9b2017-12-01 10:25:09 +0000128 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
Russell King9525ae82017-07-25 15:03:13 +0100129 if (fixed_node) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000130 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
Russell King9525ae82017-07-25 15:03:13 +0100131
132 pl->link_config.speed = speed;
133 pl->link_config.duplex = DUPLEX_HALF;
134
Russell King8fa7b9b2017-12-01 10:25:09 +0000135 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
Russell King9525ae82017-07-25 15:03:13 +0100136 pl->link_config.duplex = DUPLEX_FULL;
137
138 /* We treat the "pause" and "asym-pause" terminology as
139 * defining the link partner's ability. */
Russell King8fa7b9b2017-12-01 10:25:09 +0000140 if (fwnode_property_read_bool(fixed_node, "pause"))
Russell King9525ae82017-07-25 15:03:13 +0100141 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000142 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
Russell King9525ae82017-07-25 15:03:13 +0100143 pl->link_config.pause |= MLO_PAUSE_ASYM;
144
145 if (ret == 0) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000146 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
147 0, GPIOD_IN, "?");
Russell King9525ae82017-07-25 15:03:13 +0100148
149 if (!IS_ERR(desc))
150 pl->link_gpio = desc;
151 else if (desc == ERR_PTR(-EPROBE_DEFER))
152 ret = -EPROBE_DEFER;
153 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000154 fwnode_handle_put(fixed_node);
Russell King9525ae82017-07-25 15:03:13 +0100155
156 if (ret)
157 return ret;
158 } else {
Russell King8fa7b9b2017-12-01 10:25:09 +0000159 u32 prop[5];
160
161 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
162 NULL, 0);
163 if (ret != ARRAY_SIZE(prop)) {
Russell King9525ae82017-07-25 15:03:13 +0100164 netdev_err(pl->netdev, "broken fixed-link?\n");
165 return -EINVAL;
166 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000167
168 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
169 prop, ARRAY_SIZE(prop));
170 if (!ret) {
171 pl->link_config.duplex = prop[1] ?
Russell King9525ae82017-07-25 15:03:13 +0100172 DUPLEX_FULL : DUPLEX_HALF;
Russell King8fa7b9b2017-12-01 10:25:09 +0000173 pl->link_config.speed = prop[2];
174 if (prop[3])
Russell King9525ae82017-07-25 15:03:13 +0100175 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000176 if (prop[4])
Russell King9525ae82017-07-25 15:03:13 +0100177 pl->link_config.pause |= MLO_PAUSE_ASYM;
178 }
179 }
180
181 if (pl->link_config.speed > SPEED_1000 &&
182 pl->link_config.duplex != DUPLEX_FULL)
183 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
184 pl->link_config.speed);
185
186 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
187 linkmode_copy(pl->link_config.advertising, pl->supported);
188 phylink_validate(pl, pl->supported, &pl->link_config);
189
190 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100191 pl->supported, true);
Russell King9525ae82017-07-25 15:03:13 +0100192 linkmode_zero(pl->supported);
193 phylink_set(pl->supported, MII);
194 if (s) {
195 __set_bit(s->bit, pl->supported);
196 } else {
197 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
198 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
199 pl->link_config.speed);
200 }
201
202 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
203 pl->supported);
204
205 pl->link_config.link = 1;
206 pl->link_config.an_complete = 1;
207
208 return 0;
209}
210
Russell King8fa7b9b2017-12-01 10:25:09 +0000211static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100212{
Russell King8fa7b9b2017-12-01 10:25:09 +0000213 struct fwnode_handle *dn;
Russell King9525ae82017-07-25 15:03:13 +0100214 const char *managed;
215
Russell King8fa7b9b2017-12-01 10:25:09 +0000216 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
217 if (dn || fwnode_property_present(fwnode, "fixed-link"))
Russell King9525ae82017-07-25 15:03:13 +0100218 pl->link_an_mode = MLO_AN_FIXED;
Russell King8fa7b9b2017-12-01 10:25:09 +0000219 fwnode_handle_put(dn);
Russell King9525ae82017-07-25 15:03:13 +0100220
Russell King8fa7b9b2017-12-01 10:25:09 +0000221 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
Russell King9525ae82017-07-25 15:03:13 +0100222 strcmp(managed, "in-band-status") == 0) {
223 if (pl->link_an_mode == MLO_AN_FIXED) {
224 netdev_err(pl->netdev,
225 "can't use both fixed-link and in-band-status\n");
226 return -EINVAL;
227 }
228
229 linkmode_zero(pl->supported);
230 phylink_set(pl->supported, MII);
231 phylink_set(pl->supported, Autoneg);
232 phylink_set(pl->supported, Asym_Pause);
233 phylink_set(pl->supported, Pause);
234 pl->link_config.an_enabled = true;
Russell King86a362c2017-12-01 10:24:26 +0000235 pl->link_an_mode = MLO_AN_INBAND;
Russell King9525ae82017-07-25 15:03:13 +0100236
237 switch (pl->link_config.interface) {
238 case PHY_INTERFACE_MODE_SGMII:
239 phylink_set(pl->supported, 10baseT_Half);
240 phylink_set(pl->supported, 10baseT_Full);
241 phylink_set(pl->supported, 100baseT_Half);
242 phylink_set(pl->supported, 100baseT_Full);
243 phylink_set(pl->supported, 1000baseT_Half);
244 phylink_set(pl->supported, 1000baseT_Full);
Russell King9525ae82017-07-25 15:03:13 +0100245 break;
246
247 case PHY_INTERFACE_MODE_1000BASEX:
248 phylink_set(pl->supported, 1000baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100249 break;
250
251 case PHY_INTERFACE_MODE_2500BASEX:
252 phylink_set(pl->supported, 2500baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100253 break;
254
Russell Kingda7c1862017-07-25 15:03:34 +0100255 case PHY_INTERFACE_MODE_10GKR:
256 phylink_set(pl->supported, 10baseT_Half);
257 phylink_set(pl->supported, 10baseT_Full);
258 phylink_set(pl->supported, 100baseT_Half);
259 phylink_set(pl->supported, 100baseT_Full);
260 phylink_set(pl->supported, 1000baseT_Half);
261 phylink_set(pl->supported, 1000baseT_Full);
262 phylink_set(pl->supported, 1000baseX_Full);
263 phylink_set(pl->supported, 10000baseKR_Full);
264 phylink_set(pl->supported, 10000baseCR_Full);
265 phylink_set(pl->supported, 10000baseSR_Full);
266 phylink_set(pl->supported, 10000baseLR_Full);
267 phylink_set(pl->supported, 10000baseLRM_Full);
268 phylink_set(pl->supported, 10000baseER_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100269 break;
270
Russell King9525ae82017-07-25 15:03:13 +0100271 default:
272 netdev_err(pl->netdev,
273 "incorrect link mode %s for in-band status\n",
274 phy_modes(pl->link_config.interface));
275 return -EINVAL;
276 }
277
278 linkmode_copy(pl->link_config.advertising, pl->supported);
279
280 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
281 netdev_err(pl->netdev,
282 "failed to validate link configuration for in-band status\n");
283 return -EINVAL;
284 }
285 }
286
287 return 0;
288}
289
290static void phylink_mac_config(struct phylink *pl,
291 const struct phylink_link_state *state)
292{
293 netdev_dbg(pl->netdev,
294 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
295 __func__, phylink_an_mode_str(pl->link_an_mode),
296 phy_modes(state->interface),
297 phy_speed_to_str(state->speed),
298 phy_duplex_to_str(state->duplex),
299 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
300 state->pause, state->link, state->an_enabled);
301
302 pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
303}
304
Russell King0946cf12019-02-11 11:46:01 +0000305static void phylink_mac_config_up(struct phylink *pl,
306 const struct phylink_link_state *state)
307{
308 if (state->link)
309 phylink_mac_config(pl, state);
310}
311
Russell King9525ae82017-07-25 15:03:13 +0100312static void phylink_mac_an_restart(struct phylink *pl)
313{
314 if (pl->link_config.an_enabled &&
Russell King365c1e62017-12-01 10:24:16 +0000315 phy_interface_mode_is_8023z(pl->link_config.interface))
Russell King9525ae82017-07-25 15:03:13 +0100316 pl->ops->mac_an_restart(pl->netdev);
317}
318
319static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
320{
321 struct net_device *ndev = pl->netdev;
322
323 linkmode_copy(state->advertising, pl->link_config.advertising);
324 linkmode_zero(state->lp_advertising);
325 state->interface = pl->link_config.interface;
326 state->an_enabled = pl->link_config.an_enabled;
Heiner Kallweitd25ed412019-02-26 19:29:22 +0100327 state->speed = SPEED_UNKNOWN;
328 state->duplex = DUPLEX_UNKNOWN;
329 state->pause = MLO_PAUSE_NONE;
330 state->an_complete = 0;
Russell King9525ae82017-07-25 15:03:13 +0100331 state->link = 1;
332
333 return pl->ops->mac_link_state(ndev, state);
334}
335
336/* The fixed state is... fixed except for the link state,
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800337 * which may be determined by a GPIO or a callback.
Russell King9525ae82017-07-25 15:03:13 +0100338 */
339static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
340{
341 *state = pl->link_config;
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800342 if (pl->get_fixed_state)
343 pl->get_fixed_state(pl->netdev, state);
344 else if (pl->link_gpio)
Florian Fainellibb322a92018-05-10 13:17:29 -0700345 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
Russell King9525ae82017-07-25 15:03:13 +0100346}
347
348/* Flow control is resolved according to our and the link partners
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000349 * advertisements using the following drawn from the 802.3 specs:
Russell King9525ae82017-07-25 15:03:13 +0100350 * Local device Link partner
351 * Pause AsymDir Pause AsymDir Result
352 * 1 X 1 X TX+RX
353 * 0 1 1 1 RX
354 * 1 1 0 1 TX
355 */
356static void phylink_resolve_flow(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700357 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100358{
359 int new_pause = 0;
360
361 if (pl->link_config.pause & MLO_PAUSE_AN) {
362 int pause = 0;
363
364 if (phylink_test(pl->link_config.advertising, Pause))
365 pause |= MLO_PAUSE_SYM;
366 if (phylink_test(pl->link_config.advertising, Asym_Pause))
367 pause |= MLO_PAUSE_ASYM;
368
369 pause &= state->pause;
370
371 if (pause & MLO_PAUSE_SYM)
372 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
373 else if (pause & MLO_PAUSE_ASYM)
374 new_pause = state->pause & MLO_PAUSE_SYM ?
375 MLO_PAUSE_RX : MLO_PAUSE_TX;
376 } else {
377 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
378 }
379
380 state->pause &= ~MLO_PAUSE_TXRX_MASK;
381 state->pause |= new_pause;
382}
383
384static const char *phylink_pause_to_str(int pause)
385{
386 switch (pause & MLO_PAUSE_TXRX_MASK) {
387 case MLO_PAUSE_TX | MLO_PAUSE_RX:
388 return "rx/tx";
389 case MLO_PAUSE_TX:
390 return "tx";
391 case MLO_PAUSE_RX:
392 return "rx";
393 default:
394 return "off";
395 }
396}
397
398static void phylink_resolve(struct work_struct *w)
399{
400 struct phylink *pl = container_of(w, struct phylink, resolve);
401 struct phylink_link_state link_state;
402 struct net_device *ndev = pl->netdev;
403
404 mutex_lock(&pl->state_mutex);
405 if (pl->phylink_disable_state) {
406 pl->mac_link_dropped = false;
407 link_state.link = false;
408 } else if (pl->mac_link_dropped) {
409 link_state.link = false;
410 } else {
411 switch (pl->link_an_mode) {
412 case MLO_AN_PHY:
413 link_state = pl->phy_state;
414 phylink_resolve_flow(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000415 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100416 break;
417
418 case MLO_AN_FIXED:
419 phylink_get_fixed_state(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000420 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100421 break;
422
Russell King86a362c2017-12-01 10:24:26 +0000423 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100424 phylink_get_mac_state(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100425
Russell King406cb0c2019-05-20 16:07:20 +0100426 /* If we have a phy, the "up" state is the union of
427 * both the PHY and the MAC */
428 if (pl->phydev)
429 link_state.link &= pl->phy_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100430
Russell King406cb0c2019-05-20 16:07:20 +0100431 /* Only update if the PHY link is up */
432 if (pl->phydev && pl->phy_state.link) {
433 link_state.interface = pl->phy_state.interface;
Russell King9525ae82017-07-25 15:03:13 +0100434
Russell King406cb0c2019-05-20 16:07:20 +0100435 /* If we have a PHY, we need to update with
436 * the pause mode bits. */
437 link_state.pause |= pl->phy_state.pause;
438 phylink_resolve_flow(pl, &link_state);
439 phylink_mac_config(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100440 }
441 break;
Russell King9525ae82017-07-25 15:03:13 +0100442 }
443 }
444
445 if (link_state.link != netif_carrier_ok(ndev)) {
446 if (!link_state.link) {
447 netif_carrier_off(ndev);
Florian Fainellic6ab3002018-03-28 15:44:15 -0700448 pl->ops->mac_link_down(ndev, pl->link_an_mode,
449 pl->phy_state.interface);
Russell King9525ae82017-07-25 15:03:13 +0100450 netdev_info(ndev, "Link is Down\n");
451 } else {
452 pl->ops->mac_link_up(ndev, pl->link_an_mode,
Florian Fainellic6ab3002018-03-28 15:44:15 -0700453 pl->phy_state.interface,
Russell King9525ae82017-07-25 15:03:13 +0100454 pl->phydev);
455
456 netif_carrier_on(ndev);
457
458 netdev_info(ndev,
459 "Link is Up - %s/%s - flow control %s\n",
460 phy_speed_to_str(link_state.speed),
461 phy_duplex_to_str(link_state.duplex),
462 phylink_pause_to_str(link_state.pause));
463 }
464 }
465 if (!link_state.link && pl->mac_link_dropped) {
466 pl->mac_link_dropped = false;
467 queue_work(system_power_efficient_wq, &pl->resolve);
468 }
469 mutex_unlock(&pl->state_mutex);
470}
471
472static void phylink_run_resolve(struct phylink *pl)
473{
474 if (!pl->phylink_disable_state)
475 queue_work(system_power_efficient_wq, &pl->resolve);
476}
477
Russell King87454b62019-02-11 15:04:24 +0000478static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
479{
480 unsigned long state = pl->phylink_disable_state;
481
482 set_bit(bit, &pl->phylink_disable_state);
483 if (state == 0) {
484 queue_work(system_power_efficient_wq, &pl->resolve);
485 flush_work(&pl->resolve);
486 }
487}
488
Russell King9cd00a82018-05-10 13:17:31 -0700489static void phylink_fixed_poll(struct timer_list *t)
490{
491 struct phylink *pl = container_of(t, struct phylink, link_poll);
492
493 mod_timer(t, jiffies + HZ);
494
495 phylink_run_resolve(pl);
496}
497
Russell Kingce0aa272017-07-25 15:03:18 +0100498static const struct sfp_upstream_ops sfp_phylink_ops;
499
Russell King8fa7b9b2017-12-01 10:25:09 +0000500static int phylink_register_sfp(struct phylink *pl,
501 struct fwnode_handle *fwnode)
Russell Kingce0aa272017-07-25 15:03:18 +0100502{
Russell King8fa7b9b2017-12-01 10:25:09 +0000503 struct fwnode_reference_args ref;
504 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100505
Florian Fainelli02477802017-12-14 15:57:58 -0800506 if (!fwnode)
507 return 0;
508
Russell King8fa7b9b2017-12-01 10:25:09 +0000509 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
510 0, 0, &ref);
511 if (ret < 0) {
512 if (ret == -ENOENT)
513 return 0;
Russell Kingce0aa272017-07-25 15:03:18 +0100514
Russell King8fa7b9b2017-12-01 10:25:09 +0000515 netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
516 ret);
517 return ret;
518 }
519
520 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
Russell Kingce0aa272017-07-25 15:03:18 +0100521 &sfp_phylink_ops);
522 if (!pl->sfp_bus)
523 return -ENOMEM;
524
525 return 0;
526}
527
Russell King8796c892017-12-01 10:24:47 +0000528/**
529 * phylink_create() - create a phylink instance
530 * @ndev: a pointer to the &struct net_device
Russell King8fa7b9b2017-12-01 10:25:09 +0000531 * @fwnode: a pointer to a &struct fwnode_handle describing the network
532 * interface
Russell King8796c892017-12-01 10:24:47 +0000533 * @iface: the desired link mode defined by &typedef phy_interface_t
534 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
535 *
536 * Create a new phylink instance, and parse the link parameters found in @np.
537 * This will parse in-band modes, fixed-link or SFP configuration.
538 *
539 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
540 * must use IS_ERR() to check for errors from this function.
541 */
Russell King8fa7b9b2017-12-01 10:25:09 +0000542struct phylink *phylink_create(struct net_device *ndev,
543 struct fwnode_handle *fwnode,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700544 phy_interface_t iface,
545 const struct phylink_mac_ops *ops)
Russell King9525ae82017-07-25 15:03:13 +0100546{
547 struct phylink *pl;
548 int ret;
549
550 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
551 if (!pl)
552 return ERR_PTR(-ENOMEM);
553
554 mutex_init(&pl->state_mutex);
555 INIT_WORK(&pl->resolve, phylink_resolve);
556 pl->netdev = ndev;
557 pl->phy_state.interface = iface;
558 pl->link_interface = iface;
Florian Fainelli4be11ef2017-12-12 16:00:29 -0800559 if (iface == PHY_INTERFACE_MODE_MOCA)
560 pl->link_port = PORT_BNC;
561 else
562 pl->link_port = PORT_MII;
Russell King9525ae82017-07-25 15:03:13 +0100563 pl->link_config.interface = iface;
564 pl->link_config.pause = MLO_PAUSE_AN;
565 pl->link_config.speed = SPEED_UNKNOWN;
566 pl->link_config.duplex = DUPLEX_UNKNOWN;
Russell King74ee0e82017-12-20 23:21:34 +0000567 pl->link_config.an_enabled = true;
Russell King9525ae82017-07-25 15:03:13 +0100568 pl->ops = ops;
569 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King9cd00a82018-05-10 13:17:31 -0700570 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
Russell King9525ae82017-07-25 15:03:13 +0100571
572 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
573 linkmode_copy(pl->link_config.advertising, pl->supported);
574 phylink_validate(pl, pl->supported, &pl->link_config);
575
Russell King8fa7b9b2017-12-01 10:25:09 +0000576 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100577 if (ret < 0) {
578 kfree(pl);
579 return ERR_PTR(ret);
580 }
581
582 if (pl->link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000583 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100584 if (ret < 0) {
585 kfree(pl);
586 return ERR_PTR(ret);
587 }
588 }
589
Russell King8fa7b9b2017-12-01 10:25:09 +0000590 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100591 if (ret < 0) {
592 kfree(pl);
593 return ERR_PTR(ret);
594 }
595
Russell King9525ae82017-07-25 15:03:13 +0100596 return pl;
597}
598EXPORT_SYMBOL_GPL(phylink_create);
599
Russell King8796c892017-12-01 10:24:47 +0000600/**
601 * phylink_destroy() - cleanup and destroy the phylink instance
602 * @pl: a pointer to a &struct phylink returned from phylink_create()
603 *
604 * Destroy a phylink instance. Any PHY that has been attached must have been
605 * cleaned up via phylink_disconnect_phy() prior to calling this function.
606 */
Russell King9525ae82017-07-25 15:03:13 +0100607void phylink_destroy(struct phylink *pl)
608{
Russell Kingce0aa272017-07-25 15:03:18 +0100609 if (pl->sfp_bus)
610 sfp_unregister_upstream(pl->sfp_bus);
Florian Fainelli3bcd4772018-05-20 20:49:47 -0700611 if (!IS_ERR_OR_NULL(pl->link_gpio))
Florian Fainellidaab3342018-05-10 13:17:30 -0700612 gpiod_put(pl->link_gpio);
Russell Kingce0aa272017-07-25 15:03:18 +0100613
Russell King9525ae82017-07-25 15:03:13 +0100614 cancel_work_sync(&pl->resolve);
615 kfree(pl);
616}
617EXPORT_SYMBOL_GPL(phylink_destroy);
618
Wei Yongjun1ec6e532017-11-02 11:14:48 +0000619static void phylink_phy_change(struct phy_device *phydev, bool up,
620 bool do_carrier)
Russell King9525ae82017-07-25 15:03:13 +0100621{
622 struct phylink *pl = phydev->phylink;
623
624 mutex_lock(&pl->state_mutex);
625 pl->phy_state.speed = phydev->speed;
626 pl->phy_state.duplex = phydev->duplex;
627 pl->phy_state.pause = MLO_PAUSE_NONE;
628 if (phydev->pause)
629 pl->phy_state.pause |= MLO_PAUSE_SYM;
630 if (phydev->asym_pause)
631 pl->phy_state.pause |= MLO_PAUSE_ASYM;
632 pl->phy_state.interface = phydev->interface;
633 pl->phy_state.link = up;
634 mutex_unlock(&pl->state_mutex);
635
636 phylink_run_resolve(pl);
637
638 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
Florian Fainelli516b29e2017-10-30 21:42:57 -0700639 phy_modes(phydev->interface),
Russell King9525ae82017-07-25 15:03:13 +0100640 phy_speed_to_str(phydev->speed),
641 phy_duplex_to_str(phydev->duplex));
642}
643
644static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
645{
646 struct phylink_link_state config;
647 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
Russell King9525ae82017-07-25 15:03:13 +0100648 int ret;
649
650 memset(&config, 0, sizeof(config));
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100651 linkmode_copy(supported, phy->supported);
652 linkmode_copy(config.advertising, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100653 config.interface = pl->link_config.interface;
654
655 /*
656 * This is the new way of dealing with flow control for PHYs,
657 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
658 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
659 * using our validate call to the MAC, we rely upon the MAC
660 * clearing the bits from both supported and advertising fields.
661 */
662 if (phylink_test(supported, Pause))
663 phylink_set(config.advertising, Pause);
664 if (phylink_test(supported, Asym_Pause))
665 phylink_set(config.advertising, Asym_Pause);
666
667 ret = phylink_validate(pl, supported, &config);
668 if (ret)
669 return ret;
670
671 phy->phylink = pl;
672 phy->phy_link_change = phylink_phy_change;
673
674 netdev_info(pl->netdev,
675 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
676 phy->drv->name);
677
678 mutex_lock(&phy->lock);
679 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100680 pl->phydev = phy;
681 linkmode_copy(pl->supported, supported);
682 linkmode_copy(pl->link_config.advertising, config.advertising);
683
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000684 /* Restrict the phy advertisement according to the MAC support. */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100685 linkmode_copy(phy->advertising, config.advertising);
Russell King9525ae82017-07-25 15:03:13 +0100686 mutex_unlock(&pl->state_mutex);
687 mutex_unlock(&phy->lock);
688
689 netdev_dbg(pl->netdev,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100690 "phy: setting supported %*pb advertising %*pb\n",
Russell King9525ae82017-07-25 15:03:13 +0100691 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100692 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100693
Heiner Kallweit434a4312019-01-23 07:31:58 +0100694 if (phy_interrupt_is_valid(phy))
695 phy_request_interrupt(phy);
Russell King9525ae82017-07-25 15:03:13 +0100696
697 return 0;
698}
699
Baruch Siach7e418372018-10-03 19:04:49 +0300700static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
701 phy_interface_t interface)
702{
703 int ret;
704
705 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
706 (pl->link_an_mode == MLO_AN_INBAND &&
707 phy_interface_mode_is_8023z(interface))))
708 return -EINVAL;
709
710 if (pl->phydev)
711 return -EBUSY;
712
713 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
714 if (ret)
715 return ret;
716
717 ret = phylink_bringup_phy(pl, phy);
718 if (ret)
719 phy_detach(phy);
720
721 return ret;
722}
723
Russell King8796c892017-12-01 10:24:47 +0000724/**
725 * phylink_connect_phy() - connect a PHY to the phylink instance
726 * @pl: a pointer to a &struct phylink returned from phylink_create()
727 * @phy: a pointer to a &struct phy_device.
728 *
729 * Connect @phy to the phylink instance specified by @pl by calling
730 * phy_attach_direct(). Configure the @phy according to the MAC driver's
731 * capabilities, start the PHYLIB state machine and enable any interrupts
732 * that the PHY supports.
733 *
734 * This updates the phylink's ethtool supported and advertising link mode
735 * masks.
736 *
737 * Returns 0 on success or a negative errno.
738 */
Russell King9525ae82017-07-25 15:03:13 +0100739int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
740{
Florian Fainelli4904b6e2017-12-12 16:00:26 -0800741 /* Use PHY device/driver interface */
742 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
743 pl->link_interface = phy->interface;
744 pl->link_config.interface = pl->link_interface;
745 }
746
Baruch Siach7e418372018-10-03 19:04:49 +0300747 return __phylink_connect_phy(pl, phy, pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100748}
749EXPORT_SYMBOL_GPL(phylink_connect_phy);
750
Russell King8796c892017-12-01 10:24:47 +0000751/**
752 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
753 * @pl: a pointer to a &struct phylink returned from phylink_create()
754 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -0800755 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +0000756 *
757 * Connect the phy specified in the device node @dn to the phylink instance
758 * specified by @pl. Actions specified in phylink_connect_phy() will be
759 * performed.
760 *
761 * Returns 0 on success or a negative errno.
762 */
Florian Fainelli0a629642017-12-12 16:00:25 -0800763int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
764 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +0100765{
766 struct device_node *phy_node;
767 struct phy_device *phy_dev;
768 int ret;
769
Russell Kingcf4f2672017-12-01 10:24:21 +0000770 /* Fixed links and 802.3z are handled without needing a PHY */
771 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000772 (pl->link_an_mode == MLO_AN_INBAND &&
773 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100774 return 0;
775
776 phy_node = of_parse_phandle(dn, "phy-handle", 0);
777 if (!phy_node)
778 phy_node = of_parse_phandle(dn, "phy", 0);
779 if (!phy_node)
780 phy_node = of_parse_phandle(dn, "phy-device", 0);
781
782 if (!phy_node) {
Florian Fainellid38b4afd2017-12-12 16:00:27 -0800783 if (pl->link_an_mode == MLO_AN_PHY)
Russell King9525ae82017-07-25 15:03:13 +0100784 return -ENODEV;
Russell King9525ae82017-07-25 15:03:13 +0100785 return 0;
786 }
787
Florian Fainelli0a629642017-12-12 16:00:25 -0800788 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
789 pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100790 /* We're done with the phy_node handle */
791 of_node_put(phy_node);
792
793 if (!phy_dev)
794 return -ENODEV;
795
796 ret = phylink_bringup_phy(pl, phy_dev);
797 if (ret)
798 phy_detach(phy_dev);
799
800 return ret;
801}
802EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
803
Russell King8796c892017-12-01 10:24:47 +0000804/**
805 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
806 * instance.
807 * @pl: a pointer to a &struct phylink returned from phylink_create()
808 *
809 * Disconnect any current PHY from the phylink instance described by @pl.
810 */
Russell King9525ae82017-07-25 15:03:13 +0100811void phylink_disconnect_phy(struct phylink *pl)
812{
813 struct phy_device *phy;
814
Russell King8b874512017-12-15 16:09:47 +0000815 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100816
817 phy = pl->phydev;
818 if (phy) {
819 mutex_lock(&phy->lock);
820 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100821 pl->phydev = NULL;
822 mutex_unlock(&pl->state_mutex);
823 mutex_unlock(&phy->lock);
824 flush_work(&pl->resolve);
825
826 phy_disconnect(phy);
827 }
828}
829EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
830
Russell King8796c892017-12-01 10:24:47 +0000831/**
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800832 * phylink_fixed_state_cb() - allow setting a fixed link callback
833 * @pl: a pointer to a &struct phylink returned from phylink_create()
834 * @cb: callback to execute to determine the fixed link state.
835 *
836 * The MAC driver should call this driver when the state of its link
837 * can be determined through e.g: an out of band MMIO register.
838 */
839int phylink_fixed_state_cb(struct phylink *pl,
840 void (*cb)(struct net_device *dev,
841 struct phylink_link_state *state))
842{
843 /* It does not make sense to let the link be overriden unless we use
844 * MLO_AN_FIXED
845 */
846 if (pl->link_an_mode != MLO_AN_FIXED)
847 return -EINVAL;
848
849 mutex_lock(&pl->state_mutex);
850 pl->get_fixed_state = cb;
851 mutex_unlock(&pl->state_mutex);
852
853 return 0;
854}
855EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
856
857/**
Russell King8796c892017-12-01 10:24:47 +0000858 * phylink_mac_change() - notify phylink of a change in MAC state
859 * @pl: a pointer to a &struct phylink returned from phylink_create()
860 * @up: indicates whether the link is currently up.
861 *
862 * The MAC driver should call this driver when the state of its link
863 * changes (eg, link failure, new negotiation results, etc.)
864 */
Russell King9525ae82017-07-25 15:03:13 +0100865void phylink_mac_change(struct phylink *pl, bool up)
866{
867 if (!up)
868 pl->mac_link_dropped = true;
869 phylink_run_resolve(pl);
870 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
871}
872EXPORT_SYMBOL_GPL(phylink_mac_change);
873
Russell King8796c892017-12-01 10:24:47 +0000874/**
875 * phylink_start() - start a phylink instance
876 * @pl: a pointer to a &struct phylink returned from phylink_create()
877 *
878 * Start the phylink instance specified by @pl, configuring the MAC for the
879 * desired link mode(s) and negotiation style. This should be called from the
880 * network device driver's &struct net_device_ops ndo_open() method.
881 */
Russell King9525ae82017-07-25 15:03:13 +0100882void phylink_start(struct phylink *pl)
883{
Russell King8b874512017-12-15 16:09:47 +0000884 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100885
886 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
887 phylink_an_mode_str(pl->link_an_mode),
888 phy_modes(pl->link_config.interface));
889
Antoine Tenartaeeb2e82018-09-19 11:39:31 +0200890 /* Always set the carrier off */
891 netif_carrier_off(pl->netdev);
892
Russell King9525ae82017-07-25 15:03:13 +0100893 /* Apply the link configuration to the MAC when starting. This allows
894 * a fixed-link to start with the correct parameters, and also
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000895 * ensures that we set the appropriate advertisement for Serdes links.
Russell King9525ae82017-07-25 15:03:13 +0100896 */
897 phylink_resolve_flow(pl, &pl->link_config);
898 phylink_mac_config(pl, &pl->link_config);
899
Russell King85b43942017-12-01 10:24:42 +0000900 /* Restart autonegotiation if using 802.3z to ensure that the link
901 * parameters are properly negotiated. This is necessary for DSA
902 * switches using 802.3z negotiation to ensure they see our modes.
903 */
904 phylink_mac_an_restart(pl);
905
Russell King9525ae82017-07-25 15:03:13 +0100906 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
907 phylink_run_resolve(pl);
908
Russell King9cd00a82018-05-10 13:17:31 -0700909 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
910 mod_timer(&pl->link_poll, jiffies + HZ);
Russell Kingce0aa272017-07-25 15:03:18 +0100911 if (pl->sfp_bus)
912 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100913 if (pl->phydev)
914 phy_start(pl->phydev);
915}
916EXPORT_SYMBOL_GPL(phylink_start);
917
Russell King8796c892017-12-01 10:24:47 +0000918/**
919 * phylink_stop() - stop a phylink instance
920 * @pl: a pointer to a &struct phylink returned from phylink_create()
921 *
922 * Stop the phylink instance specified by @pl. This should be called from the
923 * network device driver's &struct net_device_ops ndo_stop() method. The
924 * network device's carrier state should not be changed prior to calling this
925 * function.
926 */
Russell King9525ae82017-07-25 15:03:13 +0100927void phylink_stop(struct phylink *pl)
928{
Russell King8b874512017-12-15 16:09:47 +0000929 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100930
931 if (pl->phydev)
932 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100933 if (pl->sfp_bus)
934 sfp_upstream_stop(pl->sfp_bus);
Russell King9cd00a82018-05-10 13:17:31 -0700935 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
936 del_timer_sync(&pl->link_poll);
Russell King9525ae82017-07-25 15:03:13 +0100937
Russell King87454b62019-02-11 15:04:24 +0000938 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
Russell King9525ae82017-07-25 15:03:13 +0100939}
940EXPORT_SYMBOL_GPL(phylink_stop);
941
Russell King8796c892017-12-01 10:24:47 +0000942/**
943 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
944 * @pl: a pointer to a &struct phylink returned from phylink_create()
945 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
946 *
947 * Read the wake on lan parameters from the PHY attached to the phylink
948 * instance specified by @pl. If no PHY is currently attached, report no
949 * support for wake on lan.
950 */
Russell King9525ae82017-07-25 15:03:13 +0100951void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
952{
Russell King8b874512017-12-15 16:09:47 +0000953 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100954
955 wol->supported = 0;
956 wol->wolopts = 0;
957
958 if (pl->phydev)
959 phy_ethtool_get_wol(pl->phydev, wol);
960}
961EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
962
Russell King8796c892017-12-01 10:24:47 +0000963/**
964 * phylink_ethtool_set_wol() - set wake on lan parameters
965 * @pl: a pointer to a &struct phylink returned from phylink_create()
966 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
967 *
968 * Set the wake on lan parameters for the PHY attached to the phylink
969 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
970 * error.
971 *
972 * Returns zero on success or negative errno code.
973 */
Russell King9525ae82017-07-25 15:03:13 +0100974int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
975{
976 int ret = -EOPNOTSUPP;
977
Russell King8b874512017-12-15 16:09:47 +0000978 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100979
980 if (pl->phydev)
981 ret = phy_ethtool_set_wol(pl->phydev, wol);
982
983 return ret;
984}
985EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
986
987static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
988{
989 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
990
991 linkmode_zero(mask);
992 phylink_set_port_modes(mask);
993
994 linkmode_and(dst, dst, mask);
995 linkmode_or(dst, dst, b);
996}
997
998static void phylink_get_ksettings(const struct phylink_link_state *state,
999 struct ethtool_link_ksettings *kset)
1000{
1001 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1002 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1003 kset->base.speed = state->speed;
1004 kset->base.duplex = state->duplex;
1005 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1006 AUTONEG_DISABLE;
1007}
1008
Russell King8796c892017-12-01 10:24:47 +00001009/**
1010 * phylink_ethtool_ksettings_get() - get the current link settings
1011 * @pl: a pointer to a &struct phylink returned from phylink_create()
1012 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1013 *
1014 * Read the current link settings for the phylink instance specified by @pl.
1015 * This will be the link settings read from the MAC, PHY or fixed link
1016 * settings depending on the current negotiation mode.
1017 */
Russell King9525ae82017-07-25 15:03:13 +01001018int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001019 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001020{
1021 struct phylink_link_state link_state;
1022
Russell King8b874512017-12-15 16:09:47 +00001023 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001024
1025 if (pl->phydev) {
1026 phy_ethtool_ksettings_get(pl->phydev, kset);
1027 } else {
1028 kset->base.port = pl->link_port;
1029 }
1030
1031 linkmode_copy(kset->link_modes.supported, pl->supported);
1032
1033 switch (pl->link_an_mode) {
1034 case MLO_AN_FIXED:
1035 /* We are using fixed settings. Report these as the
1036 * current link settings - and note that these also
1037 * represent the supported speeds/duplex/pause modes.
1038 */
1039 phylink_get_fixed_state(pl, &link_state);
1040 phylink_get_ksettings(&link_state, kset);
1041 break;
1042
Russell King86a362c2017-12-01 10:24:26 +00001043 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001044 /* If there is a phy attached, then use the reported
1045 * settings from the phy with no modification.
1046 */
1047 if (pl->phydev)
1048 break;
1049
Russell King9525ae82017-07-25 15:03:13 +01001050 phylink_get_mac_state(pl, &link_state);
1051
1052 /* The MAC is reporting the link results from its own PCS
1053 * layer via in-band status. Report these as the current
1054 * link settings.
1055 */
1056 phylink_get_ksettings(&link_state, kset);
1057 break;
1058 }
1059
1060 return 0;
1061}
1062EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1063
Russell King8796c892017-12-01 10:24:47 +00001064/**
1065 * phylink_ethtool_ksettings_set() - set the link settings
1066 * @pl: a pointer to a &struct phylink returned from phylink_create()
1067 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1068 */
Russell King9525ae82017-07-25 15:03:13 +01001069int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001070 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001071{
1072 struct ethtool_link_ksettings our_kset;
1073 struct phylink_link_state config;
1074 int ret;
1075
Russell King8b874512017-12-15 16:09:47 +00001076 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001077
1078 if (kset->base.autoneg != AUTONEG_DISABLE &&
1079 kset->base.autoneg != AUTONEG_ENABLE)
1080 return -EINVAL;
1081
1082 config = pl->link_config;
1083
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001084 /* Mask out unsupported advertisements */
Russell King9525ae82017-07-25 15:03:13 +01001085 linkmode_and(config.advertising, kset->link_modes.advertising,
1086 pl->supported);
1087
1088 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1089 if (kset->base.autoneg == AUTONEG_DISABLE) {
1090 const struct phy_setting *s;
1091
1092 /* Autonegotiation disabled, select a suitable speed and
1093 * duplex.
1094 */
1095 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001096 pl->supported, false);
Russell King9525ae82017-07-25 15:03:13 +01001097 if (!s)
1098 return -EINVAL;
1099
1100 /* If we have a fixed link (as specified by firmware), refuse
1101 * to change link parameters.
1102 */
1103 if (pl->link_an_mode == MLO_AN_FIXED &&
1104 (s->speed != pl->link_config.speed ||
1105 s->duplex != pl->link_config.duplex))
1106 return -EINVAL;
1107
1108 config.speed = s->speed;
1109 config.duplex = s->duplex;
1110 config.an_enabled = false;
1111
1112 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1113 } else {
1114 /* If we have a fixed link, refuse to enable autonegotiation */
1115 if (pl->link_an_mode == MLO_AN_FIXED)
1116 return -EINVAL;
1117
1118 config.speed = SPEED_UNKNOWN;
1119 config.duplex = DUPLEX_UNKNOWN;
1120 config.an_enabled = true;
1121
1122 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1123 }
1124
1125 if (phylink_validate(pl, pl->supported, &config))
1126 return -EINVAL;
1127
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001128 /* If autonegotiation is enabled, we must have an advertisement */
Russell King9525ae82017-07-25 15:03:13 +01001129 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1130 return -EINVAL;
1131
1132 our_kset = *kset;
1133 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1134 our_kset.base.speed = config.speed;
1135 our_kset.base.duplex = config.duplex;
1136
1137 /* If we have a PHY, configure the phy */
1138 if (pl->phydev) {
1139 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1140 if (ret)
1141 return ret;
1142 }
1143
1144 mutex_lock(&pl->state_mutex);
1145 /* Configure the MAC to match the new settings */
1146 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
Russell King182088a2017-12-20 23:21:28 +00001147 pl->link_config.interface = config.interface;
Russell King9525ae82017-07-25 15:03:13 +01001148 pl->link_config.speed = our_kset.base.speed;
1149 pl->link_config.duplex = our_kset.base.duplex;
1150 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1151
1152 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1153 phylink_mac_config(pl, &pl->link_config);
1154 phylink_mac_an_restart(pl);
1155 }
1156 mutex_unlock(&pl->state_mutex);
1157
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001158 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001159}
1160EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1161
Russell King8796c892017-12-01 10:24:47 +00001162/**
1163 * phylink_ethtool_nway_reset() - restart negotiation
1164 * @pl: a pointer to a &struct phylink returned from phylink_create()
1165 *
1166 * Restart negotiation for the phylink instance specified by @pl. This will
1167 * cause any attached phy to restart negotiation with the link partner, and
1168 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1169 * negotiation.
1170 *
1171 * Returns zero on success, or negative error code.
1172 */
Russell King9525ae82017-07-25 15:03:13 +01001173int phylink_ethtool_nway_reset(struct phylink *pl)
1174{
1175 int ret = 0;
1176
Russell King8b874512017-12-15 16:09:47 +00001177 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001178
1179 if (pl->phydev)
1180 ret = phy_restart_aneg(pl->phydev);
1181 phylink_mac_an_restart(pl);
1182
1183 return ret;
1184}
1185EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1186
Russell King8796c892017-12-01 10:24:47 +00001187/**
1188 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1189 * @pl: a pointer to a &struct phylink returned from phylink_create()
1190 * @pause: a pointer to a &struct ethtool_pauseparam
1191 */
Russell King9525ae82017-07-25 15:03:13 +01001192void phylink_ethtool_get_pauseparam(struct phylink *pl,
1193 struct ethtool_pauseparam *pause)
1194{
Russell King8b874512017-12-15 16:09:47 +00001195 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001196
1197 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1198 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1199 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1200}
1201EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1202
Russell King8796c892017-12-01 10:24:47 +00001203/**
1204 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1205 * @pl: a pointer to a &struct phylink returned from phylink_create()
1206 * @pause: a pointer to a &struct ethtool_pauseparam
1207 */
Russell King9525ae82017-07-25 15:03:13 +01001208int phylink_ethtool_set_pauseparam(struct phylink *pl,
1209 struct ethtool_pauseparam *pause)
1210{
1211 struct phylink_link_state *config = &pl->link_config;
1212
Russell King8b874512017-12-15 16:09:47 +00001213 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001214
1215 if (!phylink_test(pl->supported, Pause) &&
1216 !phylink_test(pl->supported, Asym_Pause))
1217 return -EOPNOTSUPP;
1218
1219 if (!phylink_test(pl->supported, Asym_Pause) &&
1220 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1221 return -EINVAL;
1222
1223 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1224
1225 if (pause->autoneg)
1226 config->pause |= MLO_PAUSE_AN;
1227 if (pause->rx_pause)
1228 config->pause |= MLO_PAUSE_RX;
1229 if (pause->tx_pause)
1230 config->pause |= MLO_PAUSE_TX;
1231
1232 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1233 switch (pl->link_an_mode) {
1234 case MLO_AN_PHY:
1235 /* Silently mark the carrier down, and then trigger a resolve */
1236 netif_carrier_off(pl->netdev);
1237 phylink_run_resolve(pl);
1238 break;
1239
1240 case MLO_AN_FIXED:
1241 /* Should we allow fixed links to change against the config? */
1242 phylink_resolve_flow(pl, config);
1243 phylink_mac_config(pl, config);
1244 break;
1245
Russell King86a362c2017-12-01 10:24:26 +00001246 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001247 phylink_mac_config(pl, config);
1248 phylink_mac_an_restart(pl);
1249 break;
1250 }
1251 }
1252
1253 return 0;
1254}
1255EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1256
Russell King8796c892017-12-01 10:24:47 +00001257/**
1258 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1259 * counter
1260 * @pl: a pointer to a &struct phylink returned from phylink_create().
1261 *
1262 * Read the Energy Efficient Ethernet error counter from the PHY associated
1263 * with the phylink instance specified by @pl.
1264 *
1265 * Returns positive error counter value, or negative error code.
1266 */
Russell King9525ae82017-07-25 15:03:13 +01001267int phylink_get_eee_err(struct phylink *pl)
1268{
1269 int ret = 0;
1270
Russell King8b874512017-12-15 16:09:47 +00001271 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001272
1273 if (pl->phydev)
1274 ret = phy_get_eee_err(pl->phydev);
1275
1276 return ret;
1277}
1278EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1279
Russell King8796c892017-12-01 10:24:47 +00001280/**
Russell King86e58132019-02-11 11:46:06 +00001281 * phylink_init_eee() - init and check the EEE features
1282 * @pl: a pointer to a &struct phylink returned from phylink_create()
1283 * @clk_stop_enable: allow PHY to stop receive clock
1284 *
1285 * Must be called either with RTNL held or within mac_link_up()
1286 */
1287int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1288{
1289 int ret = -EOPNOTSUPP;
1290
1291 if (pl->phydev)
1292 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1293
1294 return ret;
1295}
1296EXPORT_SYMBOL_GPL(phylink_init_eee);
1297
1298/**
Russell King8796c892017-12-01 10:24:47 +00001299 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1300 * @pl: a pointer to a &struct phylink returned from phylink_create()
1301 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1302 */
Russell King9525ae82017-07-25 15:03:13 +01001303int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1304{
1305 int ret = -EOPNOTSUPP;
1306
Russell King8b874512017-12-15 16:09:47 +00001307 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001308
1309 if (pl->phydev)
1310 ret = phy_ethtool_get_eee(pl->phydev, eee);
1311
1312 return ret;
1313}
1314EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1315
Russell King8796c892017-12-01 10:24:47 +00001316/**
1317 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1318 * @pl: a pointer to a &struct phylink returned from phylink_create()
1319 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1320 */
Russell King9525ae82017-07-25 15:03:13 +01001321int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1322{
1323 int ret = -EOPNOTSUPP;
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_ethtool_set_eee(pl->phydev, eee);
1329
1330 return ret;
1331}
1332EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1333
1334/* This emulates MII registers for a fixed-mode phy operating as per the
1335 * passed in state. "aneg" defines if we report negotiation is possible.
1336 *
1337 * FIXME: should deal with negotiation state too.
1338 */
1339static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1340 struct phylink_link_state *state, bool aneg)
1341{
1342 struct fixed_phy_status fs;
1343 int val;
1344
1345 fs.link = state->link;
1346 fs.speed = state->speed;
1347 fs.duplex = state->duplex;
1348 fs.pause = state->pause & MLO_PAUSE_SYM;
1349 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1350
1351 val = swphy_read_reg(reg, &fs);
1352 if (reg == MII_BMSR) {
1353 if (!state->an_complete)
1354 val &= ~BMSR_ANEGCOMPLETE;
1355 if (!aneg)
1356 val &= ~BMSR_ANEGCAPABLE;
1357 }
1358 return val;
1359}
1360
Russell Kingecbd87b2017-07-25 15:03:28 +01001361static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1362 unsigned int reg)
1363{
1364 struct phy_device *phydev = pl->phydev;
1365 int prtad, devad;
1366
1367 if (mdio_phy_id_is_c45(phy_id)) {
1368 prtad = mdio_phy_id_prtad(phy_id);
1369 devad = mdio_phy_id_devad(phy_id);
1370 devad = MII_ADDR_C45 | devad << 16 | reg;
1371 } else if (phydev->is_c45) {
1372 switch (reg) {
1373 case MII_BMCR:
1374 case MII_BMSR:
1375 case MII_PHYSID1:
1376 case MII_PHYSID2:
1377 devad = __ffs(phydev->c45_ids.devices_in_package);
1378 break;
1379 case MII_ADVERTISE:
1380 case MII_LPA:
1381 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1382 return -EINVAL;
1383 devad = MDIO_MMD_AN;
1384 if (reg == MII_ADVERTISE)
1385 reg = MDIO_AN_ADVERTISE;
1386 else
1387 reg = MDIO_AN_LPA;
1388 break;
1389 default:
1390 return -EINVAL;
1391 }
1392 prtad = phy_id;
1393 devad = MII_ADDR_C45 | devad << 16 | reg;
1394 } else {
1395 prtad = phy_id;
1396 devad = reg;
1397 }
1398 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1399}
1400
1401static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1402 unsigned int reg, unsigned int val)
1403{
1404 struct phy_device *phydev = pl->phydev;
1405 int prtad, devad;
1406
1407 if (mdio_phy_id_is_c45(phy_id)) {
1408 prtad = mdio_phy_id_prtad(phy_id);
1409 devad = mdio_phy_id_devad(phy_id);
1410 devad = MII_ADDR_C45 | devad << 16 | reg;
1411 } else if (phydev->is_c45) {
1412 switch (reg) {
1413 case MII_BMCR:
1414 case MII_BMSR:
1415 case MII_PHYSID1:
1416 case MII_PHYSID2:
1417 devad = __ffs(phydev->c45_ids.devices_in_package);
1418 break;
1419 case MII_ADVERTISE:
1420 case MII_LPA:
1421 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1422 return -EINVAL;
1423 devad = MDIO_MMD_AN;
1424 if (reg == MII_ADVERTISE)
1425 reg = MDIO_AN_ADVERTISE;
1426 else
1427 reg = MDIO_AN_LPA;
1428 break;
1429 default:
1430 return -EINVAL;
1431 }
1432 prtad = phy_id;
1433 devad = MII_ADDR_C45 | devad << 16 | reg;
1434 } else {
1435 prtad = phy_id;
1436 devad = reg;
1437 }
1438
1439 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1440}
1441
Russell King9525ae82017-07-25 15:03:13 +01001442static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1443 unsigned int reg)
1444{
1445 struct phylink_link_state state;
1446 int val = 0xffff;
1447
Russell King9525ae82017-07-25 15:03:13 +01001448 switch (pl->link_an_mode) {
1449 case MLO_AN_FIXED:
1450 if (phy_id == 0) {
1451 phylink_get_fixed_state(pl, &state);
1452 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1453 true);
1454 }
1455 break;
1456
1457 case MLO_AN_PHY:
1458 return -EOPNOTSUPP;
1459
Russell King86a362c2017-12-01 10:24:26 +00001460 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001461 if (phy_id == 0) {
1462 val = phylink_get_mac_state(pl, &state);
1463 if (val < 0)
1464 return val;
1465
1466 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1467 true);
1468 }
1469 break;
1470 }
1471
1472 return val & 0xffff;
1473}
1474
1475static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1476 unsigned int reg, unsigned int val)
1477{
Russell King9525ae82017-07-25 15:03:13 +01001478 switch (pl->link_an_mode) {
1479 case MLO_AN_FIXED:
1480 break;
1481
1482 case MLO_AN_PHY:
1483 return -EOPNOTSUPP;
1484
Russell King86a362c2017-12-01 10:24:26 +00001485 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001486 break;
1487 }
1488
1489 return 0;
1490}
1491
Russell King8796c892017-12-01 10:24:47 +00001492/**
1493 * phylink_mii_ioctl() - generic mii ioctl interface
1494 * @pl: a pointer to a &struct phylink returned from phylink_create()
1495 * @ifr: a pointer to a &struct ifreq for socket ioctls
1496 * @cmd: ioctl cmd to execute
1497 *
1498 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1499 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1500 *
1501 * Returns: zero on success or negative error code.
1502 *
1503 * %SIOCGMIIPHY:
1504 * read register from the current PHY.
1505 * %SIOCGMIIREG:
1506 * read register from the specified PHY.
1507 * %SIOCSMIIREG:
1508 * set a register on the specified PHY.
1509 */
Russell King9525ae82017-07-25 15:03:13 +01001510int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1511{
Russell Kingecbd87b2017-07-25 15:03:28 +01001512 struct mii_ioctl_data *mii = if_mii(ifr);
1513 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001514
Russell King8b874512017-12-15 16:09:47 +00001515 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001516
Russell Kingecbd87b2017-07-25 15:03:28 +01001517 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001518 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001519 switch (cmd) {
1520 case SIOCGMIIPHY:
1521 mii->phy_id = pl->phydev->mdio.addr;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001522 /* fall through */
Russell King9525ae82017-07-25 15:03:13 +01001523
Russell Kingecbd87b2017-07-25 15:03:28 +01001524 case SIOCGMIIREG:
1525 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1526 if (ret >= 0) {
1527 mii->val_out = ret;
1528 ret = 0;
1529 }
1530 break;
Russell King9525ae82017-07-25 15:03:13 +01001531
Russell Kingecbd87b2017-07-25 15:03:28 +01001532 case SIOCSMIIREG:
1533 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1534 mii->val_in);
1535 break;
Russell King9525ae82017-07-25 15:03:13 +01001536
Russell Kingecbd87b2017-07-25 15:03:28 +01001537 default:
Russell King9525ae82017-07-25 15:03:13 +01001538 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001539 break;
1540 }
1541 } else {
1542 switch (cmd) {
1543 case SIOCGMIIPHY:
1544 mii->phy_id = 0;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001545 /* fall through */
Russell Kingecbd87b2017-07-25 15:03:28 +01001546
1547 case SIOCGMIIREG:
1548 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1549 if (ret >= 0) {
1550 mii->val_out = ret;
1551 ret = 0;
1552 }
1553 break;
1554
1555 case SIOCSMIIREG:
1556 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1557 mii->val_in);
1558 break;
1559
1560 default:
1561 ret = -EOPNOTSUPP;
1562 break;
1563 }
Russell King9525ae82017-07-25 15:03:13 +01001564 }
1565
1566 return ret;
1567}
1568EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1569
Russell Kingce0aa272017-07-25 15:03:18 +01001570static int phylink_sfp_module_insert(void *upstream,
1571 const struct sfp_eeprom_id *id)
1572{
1573 struct phylink *pl = upstream;
1574 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1575 struct phylink_link_state config;
1576 phy_interface_t iface;
Russell King444d3502017-12-29 12:15:33 +00001577 int ret = 0;
Russell Kingce0aa272017-07-25 15:03:18 +01001578 bool changed;
1579 u8 port;
1580
Russell King8b874512017-12-15 16:09:47 +00001581 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001582
Russell Kinga9c79362018-02-27 15:53:02 +00001583 sfp_parse_support(pl->sfp_bus, id, support);
1584 port = sfp_parse_port(pl->sfp_bus, id, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001585
1586 memset(&config, 0, sizeof(config));
1587 linkmode_copy(config.advertising, support);
Russell Kinga9c79362018-02-27 15:53:02 +00001588 config.interface = PHY_INTERFACE_MODE_NA;
Russell Kingce0aa272017-07-25 15:03:18 +01001589 config.speed = SPEED_UNKNOWN;
1590 config.duplex = DUPLEX_UNKNOWN;
1591 config.pause = MLO_PAUSE_AN;
1592 config.an_enabled = pl->link_config.an_enabled;
1593
1594 /* Ignore errors if we're expecting a PHY to attach later */
1595 ret = phylink_validate(pl, support, &config);
1596 if (ret) {
Russell Kinga9c79362018-02-27 15:53:02 +00001597 netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
1598 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1599 return ret;
1600 }
1601
1602 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1603 if (iface == PHY_INTERFACE_MODE_NA) {
1604 netdev_err(pl->netdev,
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001605 "selection of interface failed, advertisement %*pb\n",
Russell Kinga9c79362018-02-27 15:53:02 +00001606 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1607 return -EINVAL;
1608 }
1609
1610 config.interface = iface;
1611 ret = phylink_validate(pl, support, &config);
1612 if (ret) {
Russell Kingce0aa272017-07-25 15:03:18 +01001613 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
Russell King444d3502017-12-29 12:15:33 +00001614 phylink_an_mode_str(MLO_AN_INBAND),
1615 phy_modes(config.interface),
Russell Kingce0aa272017-07-25 15:03:18 +01001616 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1617 return ret;
1618 }
1619
1620 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
Russell King444d3502017-12-29 12:15:33 +00001621 phylink_an_mode_str(MLO_AN_INBAND),
1622 phy_modes(config.interface),
Russell Kingce0aa272017-07-25 15:03:18 +01001623 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1624
Russell King86a362c2017-12-01 10:24:26 +00001625 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001626 return -EINVAL;
1627
1628 changed = !bitmap_equal(pl->supported, support,
1629 __ETHTOOL_LINK_MODE_MASK_NBITS);
1630 if (changed) {
1631 linkmode_copy(pl->supported, support);
1632 linkmode_copy(pl->link_config.advertising, config.advertising);
1633 }
1634
Russell King444d3502017-12-29 12:15:33 +00001635 if (pl->link_an_mode != MLO_AN_INBAND ||
Russell Kingce0aa272017-07-25 15:03:18 +01001636 pl->link_config.interface != config.interface) {
1637 pl->link_config.interface = config.interface;
Russell King444d3502017-12-29 12:15:33 +00001638 pl->link_an_mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001639
1640 changed = true;
1641
1642 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
Russell King444d3502017-12-29 12:15:33 +00001643 phylink_an_mode_str(MLO_AN_INBAND),
Russell Kingce0aa272017-07-25 15:03:18 +01001644 phy_modes(config.interface));
1645 }
1646
1647 pl->link_port = port;
1648
1649 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1650 &pl->phylink_disable_state))
1651 phylink_mac_config(pl, &pl->link_config);
1652
1653 return ret;
1654}
1655
1656static void phylink_sfp_link_down(void *upstream)
1657{
1658 struct phylink *pl = upstream;
1659
Russell King8b874512017-12-15 16:09:47 +00001660 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001661
Russell King87454b62019-02-11 15:04:24 +00001662 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
Russell Kingce0aa272017-07-25 15:03:18 +01001663}
1664
1665static void phylink_sfp_link_up(void *upstream)
1666{
1667 struct phylink *pl = upstream;
1668
Russell King8b874512017-12-15 16:09:47 +00001669 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001670
1671 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1672 phylink_run_resolve(pl);
1673}
1674
1675static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1676{
Baruch Siach7e418372018-10-03 19:04:49 +03001677 struct phylink *pl = upstream;
1678
1679 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
Russell Kingce0aa272017-07-25 15:03:18 +01001680}
1681
1682static void phylink_sfp_disconnect_phy(void *upstream)
1683{
1684 phylink_disconnect_phy(upstream);
1685}
1686
1687static const struct sfp_upstream_ops sfp_phylink_ops = {
1688 .module_insert = phylink_sfp_module_insert,
1689 .link_up = phylink_sfp_link_up,
1690 .link_down = phylink_sfp_link_down,
1691 .connect_phy = phylink_sfp_connect_phy,
1692 .disconnect_phy = phylink_sfp_disconnect_phy,
1693};
1694
Russell King624c0f02018-08-09 15:38:38 +02001695/* Helpers for MAC drivers */
1696
1697/**
1698 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1699 * @state: a pointer to a &struct phylink_link_state
1700 *
1701 * Inspect the interface mode, advertising mask or forced speed and
1702 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1703 * the interface mode to suit. @state->interface is appropriately
1704 * updated, and the advertising mask has the "other" baseX_Full flag
1705 * cleared.
1706 */
1707void phylink_helper_basex_speed(struct phylink_link_state *state)
1708{
1709 if (phy_interface_mode_is_8023z(state->interface)) {
1710 bool want_2500 = state->an_enabled ?
1711 phylink_test(state->advertising, 2500baseX_Full) :
1712 state->speed == SPEED_2500;
1713
1714 if (want_2500) {
1715 phylink_clear(state->advertising, 1000baseX_Full);
1716 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1717 } else {
1718 phylink_clear(state->advertising, 2500baseX_Full);
1719 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1720 }
1721 }
1722}
1723EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1724
Andrew Lunn5f857572019-01-21 19:10:19 +01001725MODULE_LICENSE("GPL v2");