blob: 33f66dcd369a67da39c6e3d482961531bedadf9e [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;
327 state->link = 1;
328
329 return pl->ops->mac_link_state(ndev, state);
330}
331
332/* The fixed state is... fixed except for the link state,
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800333 * which may be determined by a GPIO or a callback.
Russell King9525ae82017-07-25 15:03:13 +0100334 */
335static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
336{
337 *state = pl->link_config;
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800338 if (pl->get_fixed_state)
339 pl->get_fixed_state(pl->netdev, state);
340 else if (pl->link_gpio)
Florian Fainellibb322a92018-05-10 13:17:29 -0700341 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
Russell King9525ae82017-07-25 15:03:13 +0100342}
343
344/* Flow control is resolved according to our and the link partners
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000345 * advertisements using the following drawn from the 802.3 specs:
Russell King9525ae82017-07-25 15:03:13 +0100346 * Local device Link partner
347 * Pause AsymDir Pause AsymDir Result
348 * 1 X 1 X TX+RX
349 * 0 1 1 1 RX
350 * 1 1 0 1 TX
351 */
352static void phylink_resolve_flow(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700353 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100354{
355 int new_pause = 0;
356
357 if (pl->link_config.pause & MLO_PAUSE_AN) {
358 int pause = 0;
359
360 if (phylink_test(pl->link_config.advertising, Pause))
361 pause |= MLO_PAUSE_SYM;
362 if (phylink_test(pl->link_config.advertising, Asym_Pause))
363 pause |= MLO_PAUSE_ASYM;
364
365 pause &= state->pause;
366
367 if (pause & MLO_PAUSE_SYM)
368 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
369 else if (pause & MLO_PAUSE_ASYM)
370 new_pause = state->pause & MLO_PAUSE_SYM ?
371 MLO_PAUSE_RX : MLO_PAUSE_TX;
372 } else {
373 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
374 }
375
376 state->pause &= ~MLO_PAUSE_TXRX_MASK;
377 state->pause |= new_pause;
378}
379
380static const char *phylink_pause_to_str(int pause)
381{
382 switch (pause & MLO_PAUSE_TXRX_MASK) {
383 case MLO_PAUSE_TX | MLO_PAUSE_RX:
384 return "rx/tx";
385 case MLO_PAUSE_TX:
386 return "tx";
387 case MLO_PAUSE_RX:
388 return "rx";
389 default:
390 return "off";
391 }
392}
393
394static void phylink_resolve(struct work_struct *w)
395{
396 struct phylink *pl = container_of(w, struct phylink, resolve);
397 struct phylink_link_state link_state;
398 struct net_device *ndev = pl->netdev;
399
400 mutex_lock(&pl->state_mutex);
401 if (pl->phylink_disable_state) {
402 pl->mac_link_dropped = false;
403 link_state.link = false;
404 } else if (pl->mac_link_dropped) {
405 link_state.link = false;
406 } else {
407 switch (pl->link_an_mode) {
408 case MLO_AN_PHY:
409 link_state = pl->phy_state;
410 phylink_resolve_flow(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000411 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100412 break;
413
414 case MLO_AN_FIXED:
415 phylink_get_fixed_state(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000416 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100417 break;
418
Russell King86a362c2017-12-01 10:24:26 +0000419 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100420 phylink_get_mac_state(pl, &link_state);
421 if (pl->phydev) {
422 bool changed = false;
423
424 link_state.link = link_state.link &&
425 pl->phy_state.link;
426
427 if (pl->phy_state.interface !=
428 link_state.interface) {
429 link_state.interface = pl->phy_state.interface;
430 changed = true;
431 }
432
433 /* Propagate the flow control from the PHY
434 * to the MAC. Also propagate the interface
435 * if changed.
436 */
437 if (pl->phy_state.link || changed) {
438 link_state.pause |= pl->phy_state.pause;
439 phylink_resolve_flow(pl, &link_state);
440
441 phylink_mac_config(pl, &link_state);
442 }
443 }
444 break;
Russell King9525ae82017-07-25 15:03:13 +0100445 }
446 }
447
448 if (link_state.link != netif_carrier_ok(ndev)) {
449 if (!link_state.link) {
450 netif_carrier_off(ndev);
Florian Fainellic6ab3002018-03-28 15:44:15 -0700451 pl->ops->mac_link_down(ndev, pl->link_an_mode,
452 pl->phy_state.interface);
Russell King9525ae82017-07-25 15:03:13 +0100453 netdev_info(ndev, "Link is Down\n");
454 } else {
455 pl->ops->mac_link_up(ndev, pl->link_an_mode,
Florian Fainellic6ab3002018-03-28 15:44:15 -0700456 pl->phy_state.interface,
Russell King9525ae82017-07-25 15:03:13 +0100457 pl->phydev);
458
459 netif_carrier_on(ndev);
460
461 netdev_info(ndev,
462 "Link is Up - %s/%s - flow control %s\n",
463 phy_speed_to_str(link_state.speed),
464 phy_duplex_to_str(link_state.duplex),
465 phylink_pause_to_str(link_state.pause));
466 }
467 }
468 if (!link_state.link && pl->mac_link_dropped) {
469 pl->mac_link_dropped = false;
470 queue_work(system_power_efficient_wq, &pl->resolve);
471 }
472 mutex_unlock(&pl->state_mutex);
473}
474
475static void phylink_run_resolve(struct phylink *pl)
476{
477 if (!pl->phylink_disable_state)
478 queue_work(system_power_efficient_wq, &pl->resolve);
479}
480
Russell King9cd00a82018-05-10 13:17:31 -0700481static void phylink_fixed_poll(struct timer_list *t)
482{
483 struct phylink *pl = container_of(t, struct phylink, link_poll);
484
485 mod_timer(t, jiffies + HZ);
486
487 phylink_run_resolve(pl);
488}
489
Russell Kingce0aa272017-07-25 15:03:18 +0100490static const struct sfp_upstream_ops sfp_phylink_ops;
491
Russell King8fa7b9b2017-12-01 10:25:09 +0000492static int phylink_register_sfp(struct phylink *pl,
493 struct fwnode_handle *fwnode)
Russell Kingce0aa272017-07-25 15:03:18 +0100494{
Russell King8fa7b9b2017-12-01 10:25:09 +0000495 struct fwnode_reference_args ref;
496 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100497
Florian Fainelli02477802017-12-14 15:57:58 -0800498 if (!fwnode)
499 return 0;
500
Russell King8fa7b9b2017-12-01 10:25:09 +0000501 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
502 0, 0, &ref);
503 if (ret < 0) {
504 if (ret == -ENOENT)
505 return 0;
Russell Kingce0aa272017-07-25 15:03:18 +0100506
Russell King8fa7b9b2017-12-01 10:25:09 +0000507 netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
508 ret);
509 return ret;
510 }
511
512 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
Russell Kingce0aa272017-07-25 15:03:18 +0100513 &sfp_phylink_ops);
514 if (!pl->sfp_bus)
515 return -ENOMEM;
516
517 return 0;
518}
519
Russell King8796c892017-12-01 10:24:47 +0000520/**
521 * phylink_create() - create a phylink instance
522 * @ndev: a pointer to the &struct net_device
Russell King8fa7b9b2017-12-01 10:25:09 +0000523 * @fwnode: a pointer to a &struct fwnode_handle describing the network
524 * interface
Russell King8796c892017-12-01 10:24:47 +0000525 * @iface: the desired link mode defined by &typedef phy_interface_t
526 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
527 *
528 * Create a new phylink instance, and parse the link parameters found in @np.
529 * This will parse in-band modes, fixed-link or SFP configuration.
530 *
531 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
532 * must use IS_ERR() to check for errors from this function.
533 */
Russell King8fa7b9b2017-12-01 10:25:09 +0000534struct phylink *phylink_create(struct net_device *ndev,
535 struct fwnode_handle *fwnode,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700536 phy_interface_t iface,
537 const struct phylink_mac_ops *ops)
Russell King9525ae82017-07-25 15:03:13 +0100538{
539 struct phylink *pl;
540 int ret;
541
542 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
543 if (!pl)
544 return ERR_PTR(-ENOMEM);
545
546 mutex_init(&pl->state_mutex);
547 INIT_WORK(&pl->resolve, phylink_resolve);
548 pl->netdev = ndev;
549 pl->phy_state.interface = iface;
550 pl->link_interface = iface;
Florian Fainelli4be11ef2017-12-12 16:00:29 -0800551 if (iface == PHY_INTERFACE_MODE_MOCA)
552 pl->link_port = PORT_BNC;
553 else
554 pl->link_port = PORT_MII;
Russell King9525ae82017-07-25 15:03:13 +0100555 pl->link_config.interface = iface;
556 pl->link_config.pause = MLO_PAUSE_AN;
557 pl->link_config.speed = SPEED_UNKNOWN;
558 pl->link_config.duplex = DUPLEX_UNKNOWN;
Russell King74ee0e82017-12-20 23:21:34 +0000559 pl->link_config.an_enabled = true;
Russell King9525ae82017-07-25 15:03:13 +0100560 pl->ops = ops;
561 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King9cd00a82018-05-10 13:17:31 -0700562 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
Russell King9525ae82017-07-25 15:03:13 +0100563
564 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
565 linkmode_copy(pl->link_config.advertising, pl->supported);
566 phylink_validate(pl, pl->supported, &pl->link_config);
567
Russell King8fa7b9b2017-12-01 10:25:09 +0000568 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100569 if (ret < 0) {
570 kfree(pl);
571 return ERR_PTR(ret);
572 }
573
574 if (pl->link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000575 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100576 if (ret < 0) {
577 kfree(pl);
578 return ERR_PTR(ret);
579 }
580 }
581
Russell King8fa7b9b2017-12-01 10:25:09 +0000582 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100583 if (ret < 0) {
584 kfree(pl);
585 return ERR_PTR(ret);
586 }
587
Russell King9525ae82017-07-25 15:03:13 +0100588 return pl;
589}
590EXPORT_SYMBOL_GPL(phylink_create);
591
Russell King8796c892017-12-01 10:24:47 +0000592/**
593 * phylink_destroy() - cleanup and destroy the phylink instance
594 * @pl: a pointer to a &struct phylink returned from phylink_create()
595 *
596 * Destroy a phylink instance. Any PHY that has been attached must have been
597 * cleaned up via phylink_disconnect_phy() prior to calling this function.
598 */
Russell King9525ae82017-07-25 15:03:13 +0100599void phylink_destroy(struct phylink *pl)
600{
Russell Kingce0aa272017-07-25 15:03:18 +0100601 if (pl->sfp_bus)
602 sfp_unregister_upstream(pl->sfp_bus);
Florian Fainelli3bcd4772018-05-20 20:49:47 -0700603 if (!IS_ERR_OR_NULL(pl->link_gpio))
Florian Fainellidaab3342018-05-10 13:17:30 -0700604 gpiod_put(pl->link_gpio);
Russell Kingce0aa272017-07-25 15:03:18 +0100605
Russell King9525ae82017-07-25 15:03:13 +0100606 cancel_work_sync(&pl->resolve);
607 kfree(pl);
608}
609EXPORT_SYMBOL_GPL(phylink_destroy);
610
Wei Yongjun1ec6e532017-11-02 11:14:48 +0000611static void phylink_phy_change(struct phy_device *phydev, bool up,
612 bool do_carrier)
Russell King9525ae82017-07-25 15:03:13 +0100613{
614 struct phylink *pl = phydev->phylink;
615
616 mutex_lock(&pl->state_mutex);
617 pl->phy_state.speed = phydev->speed;
618 pl->phy_state.duplex = phydev->duplex;
619 pl->phy_state.pause = MLO_PAUSE_NONE;
620 if (phydev->pause)
621 pl->phy_state.pause |= MLO_PAUSE_SYM;
622 if (phydev->asym_pause)
623 pl->phy_state.pause |= MLO_PAUSE_ASYM;
624 pl->phy_state.interface = phydev->interface;
625 pl->phy_state.link = up;
626 mutex_unlock(&pl->state_mutex);
627
628 phylink_run_resolve(pl);
629
630 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
Florian Fainelli516b29e2017-10-30 21:42:57 -0700631 phy_modes(phydev->interface),
Russell King9525ae82017-07-25 15:03:13 +0100632 phy_speed_to_str(phydev->speed),
633 phy_duplex_to_str(phydev->duplex));
634}
635
636static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
637{
638 struct phylink_link_state config;
639 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
Russell King9525ae82017-07-25 15:03:13 +0100640 int ret;
641
642 memset(&config, 0, sizeof(config));
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100643 linkmode_copy(supported, phy->supported);
644 linkmode_copy(config.advertising, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100645 config.interface = pl->link_config.interface;
646
647 /*
648 * This is the new way of dealing with flow control for PHYs,
649 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
650 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
651 * using our validate call to the MAC, we rely upon the MAC
652 * clearing the bits from both supported and advertising fields.
653 */
654 if (phylink_test(supported, Pause))
655 phylink_set(config.advertising, Pause);
656 if (phylink_test(supported, Asym_Pause))
657 phylink_set(config.advertising, Asym_Pause);
658
659 ret = phylink_validate(pl, supported, &config);
660 if (ret)
661 return ret;
662
663 phy->phylink = pl;
664 phy->phy_link_change = phylink_phy_change;
665
666 netdev_info(pl->netdev,
667 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
668 phy->drv->name);
669
670 mutex_lock(&phy->lock);
671 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100672 pl->phydev = phy;
673 linkmode_copy(pl->supported, supported);
674 linkmode_copy(pl->link_config.advertising, config.advertising);
675
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000676 /* Restrict the phy advertisement according to the MAC support. */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100677 linkmode_copy(phy->advertising, config.advertising);
Russell King9525ae82017-07-25 15:03:13 +0100678 mutex_unlock(&pl->state_mutex);
679 mutex_unlock(&phy->lock);
680
681 netdev_dbg(pl->netdev,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100682 "phy: setting supported %*pb advertising %*pb\n",
Russell King9525ae82017-07-25 15:03:13 +0100683 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100684 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100685
Heiner Kallweit434a4312019-01-23 07:31:58 +0100686 if (phy_interrupt_is_valid(phy))
687 phy_request_interrupt(phy);
Russell King9525ae82017-07-25 15:03:13 +0100688
689 return 0;
690}
691
Baruch Siach7e418372018-10-03 19:04:49 +0300692static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
693 phy_interface_t interface)
694{
695 int ret;
696
697 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
698 (pl->link_an_mode == MLO_AN_INBAND &&
699 phy_interface_mode_is_8023z(interface))))
700 return -EINVAL;
701
702 if (pl->phydev)
703 return -EBUSY;
704
705 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
706 if (ret)
707 return ret;
708
709 ret = phylink_bringup_phy(pl, phy);
710 if (ret)
711 phy_detach(phy);
712
713 return ret;
714}
715
Russell King8796c892017-12-01 10:24:47 +0000716/**
717 * phylink_connect_phy() - connect a PHY to the phylink instance
718 * @pl: a pointer to a &struct phylink returned from phylink_create()
719 * @phy: a pointer to a &struct phy_device.
720 *
721 * Connect @phy to the phylink instance specified by @pl by calling
722 * phy_attach_direct(). Configure the @phy according to the MAC driver's
723 * capabilities, start the PHYLIB state machine and enable any interrupts
724 * that the PHY supports.
725 *
726 * This updates the phylink's ethtool supported and advertising link mode
727 * masks.
728 *
729 * Returns 0 on success or a negative errno.
730 */
Russell King9525ae82017-07-25 15:03:13 +0100731int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
732{
Florian Fainelli4904b6e2017-12-12 16:00:26 -0800733 /* Use PHY device/driver interface */
734 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
735 pl->link_interface = phy->interface;
736 pl->link_config.interface = pl->link_interface;
737 }
738
Baruch Siach7e418372018-10-03 19:04:49 +0300739 return __phylink_connect_phy(pl, phy, pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100740}
741EXPORT_SYMBOL_GPL(phylink_connect_phy);
742
Russell King8796c892017-12-01 10:24:47 +0000743/**
744 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
745 * @pl: a pointer to a &struct phylink returned from phylink_create()
746 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -0800747 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +0000748 *
749 * Connect the phy specified in the device node @dn to the phylink instance
750 * specified by @pl. Actions specified in phylink_connect_phy() will be
751 * performed.
752 *
753 * Returns 0 on success or a negative errno.
754 */
Florian Fainelli0a629642017-12-12 16:00:25 -0800755int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
756 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +0100757{
758 struct device_node *phy_node;
759 struct phy_device *phy_dev;
760 int ret;
761
Russell Kingcf4f2672017-12-01 10:24:21 +0000762 /* Fixed links and 802.3z are handled without needing a PHY */
763 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000764 (pl->link_an_mode == MLO_AN_INBAND &&
765 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100766 return 0;
767
768 phy_node = of_parse_phandle(dn, "phy-handle", 0);
769 if (!phy_node)
770 phy_node = of_parse_phandle(dn, "phy", 0);
771 if (!phy_node)
772 phy_node = of_parse_phandle(dn, "phy-device", 0);
773
774 if (!phy_node) {
Florian Fainellid38b4afd2017-12-12 16:00:27 -0800775 if (pl->link_an_mode == MLO_AN_PHY)
Russell King9525ae82017-07-25 15:03:13 +0100776 return -ENODEV;
Russell King9525ae82017-07-25 15:03:13 +0100777 return 0;
778 }
779
Florian Fainelli0a629642017-12-12 16:00:25 -0800780 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
781 pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100782 /* We're done with the phy_node handle */
783 of_node_put(phy_node);
784
785 if (!phy_dev)
786 return -ENODEV;
787
788 ret = phylink_bringup_phy(pl, phy_dev);
789 if (ret)
790 phy_detach(phy_dev);
791
792 return ret;
793}
794EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
795
Russell King8796c892017-12-01 10:24:47 +0000796/**
797 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
798 * instance.
799 * @pl: a pointer to a &struct phylink returned from phylink_create()
800 *
801 * Disconnect any current PHY from the phylink instance described by @pl.
802 */
Russell King9525ae82017-07-25 15:03:13 +0100803void phylink_disconnect_phy(struct phylink *pl)
804{
805 struct phy_device *phy;
806
Russell King8b874512017-12-15 16:09:47 +0000807 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100808
809 phy = pl->phydev;
810 if (phy) {
811 mutex_lock(&phy->lock);
812 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100813 pl->phydev = NULL;
814 mutex_unlock(&pl->state_mutex);
815 mutex_unlock(&phy->lock);
816 flush_work(&pl->resolve);
817
818 phy_disconnect(phy);
819 }
820}
821EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
822
Russell King8796c892017-12-01 10:24:47 +0000823/**
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800824 * phylink_fixed_state_cb() - allow setting a fixed link callback
825 * @pl: a pointer to a &struct phylink returned from phylink_create()
826 * @cb: callback to execute to determine the fixed link state.
827 *
828 * The MAC driver should call this driver when the state of its link
829 * can be determined through e.g: an out of band MMIO register.
830 */
831int phylink_fixed_state_cb(struct phylink *pl,
832 void (*cb)(struct net_device *dev,
833 struct phylink_link_state *state))
834{
835 /* It does not make sense to let the link be overriden unless we use
836 * MLO_AN_FIXED
837 */
838 if (pl->link_an_mode != MLO_AN_FIXED)
839 return -EINVAL;
840
841 mutex_lock(&pl->state_mutex);
842 pl->get_fixed_state = cb;
843 mutex_unlock(&pl->state_mutex);
844
845 return 0;
846}
847EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
848
849/**
Russell King8796c892017-12-01 10:24:47 +0000850 * phylink_mac_change() - notify phylink of a change in MAC state
851 * @pl: a pointer to a &struct phylink returned from phylink_create()
852 * @up: indicates whether the link is currently up.
853 *
854 * The MAC driver should call this driver when the state of its link
855 * changes (eg, link failure, new negotiation results, etc.)
856 */
Russell King9525ae82017-07-25 15:03:13 +0100857void phylink_mac_change(struct phylink *pl, bool up)
858{
859 if (!up)
860 pl->mac_link_dropped = true;
861 phylink_run_resolve(pl);
862 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
863}
864EXPORT_SYMBOL_GPL(phylink_mac_change);
865
Russell King8796c892017-12-01 10:24:47 +0000866/**
867 * phylink_start() - start a phylink instance
868 * @pl: a pointer to a &struct phylink returned from phylink_create()
869 *
870 * Start the phylink instance specified by @pl, configuring the MAC for the
871 * desired link mode(s) and negotiation style. This should be called from the
872 * network device driver's &struct net_device_ops ndo_open() method.
873 */
Russell King9525ae82017-07-25 15:03:13 +0100874void phylink_start(struct phylink *pl)
875{
Russell King8b874512017-12-15 16:09:47 +0000876 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100877
878 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
879 phylink_an_mode_str(pl->link_an_mode),
880 phy_modes(pl->link_config.interface));
881
Antoine Tenartaeeb2e82018-09-19 11:39:31 +0200882 /* Always set the carrier off */
883 netif_carrier_off(pl->netdev);
884
Russell King9525ae82017-07-25 15:03:13 +0100885 /* Apply the link configuration to the MAC when starting. This allows
886 * a fixed-link to start with the correct parameters, and also
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000887 * ensures that we set the appropriate advertisement for Serdes links.
Russell King9525ae82017-07-25 15:03:13 +0100888 */
889 phylink_resolve_flow(pl, &pl->link_config);
890 phylink_mac_config(pl, &pl->link_config);
891
Russell King85b43942017-12-01 10:24:42 +0000892 /* Restart autonegotiation if using 802.3z to ensure that the link
893 * parameters are properly negotiated. This is necessary for DSA
894 * switches using 802.3z negotiation to ensure they see our modes.
895 */
896 phylink_mac_an_restart(pl);
897
Russell King9525ae82017-07-25 15:03:13 +0100898 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
899 phylink_run_resolve(pl);
900
Russell King9cd00a82018-05-10 13:17:31 -0700901 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
902 mod_timer(&pl->link_poll, jiffies + HZ);
Russell Kingce0aa272017-07-25 15:03:18 +0100903 if (pl->sfp_bus)
904 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100905 if (pl->phydev)
906 phy_start(pl->phydev);
907}
908EXPORT_SYMBOL_GPL(phylink_start);
909
Russell King8796c892017-12-01 10:24:47 +0000910/**
911 * phylink_stop() - stop a phylink instance
912 * @pl: a pointer to a &struct phylink returned from phylink_create()
913 *
914 * Stop the phylink instance specified by @pl. This should be called from the
915 * network device driver's &struct net_device_ops ndo_stop() method. The
916 * network device's carrier state should not be changed prior to calling this
917 * function.
918 */
Russell King9525ae82017-07-25 15:03:13 +0100919void phylink_stop(struct phylink *pl)
920{
Russell King8b874512017-12-15 16:09:47 +0000921 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100922
923 if (pl->phydev)
924 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100925 if (pl->sfp_bus)
926 sfp_upstream_stop(pl->sfp_bus);
Russell King9cd00a82018-05-10 13:17:31 -0700927 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
928 del_timer_sync(&pl->link_poll);
Russell King9525ae82017-07-25 15:03:13 +0100929
930 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King2012b7d2017-11-30 13:59:26 +0000931 queue_work(system_power_efficient_wq, &pl->resolve);
Russell King9525ae82017-07-25 15:03:13 +0100932 flush_work(&pl->resolve);
933}
934EXPORT_SYMBOL_GPL(phylink_stop);
935
Russell King8796c892017-12-01 10:24:47 +0000936/**
937 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
938 * @pl: a pointer to a &struct phylink returned from phylink_create()
939 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
940 *
941 * Read the wake on lan parameters from the PHY attached to the phylink
942 * instance specified by @pl. If no PHY is currently attached, report no
943 * support for wake on lan.
944 */
Russell King9525ae82017-07-25 15:03:13 +0100945void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
946{
Russell King8b874512017-12-15 16:09:47 +0000947 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100948
949 wol->supported = 0;
950 wol->wolopts = 0;
951
952 if (pl->phydev)
953 phy_ethtool_get_wol(pl->phydev, wol);
954}
955EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
956
Russell King8796c892017-12-01 10:24:47 +0000957/**
958 * phylink_ethtool_set_wol() - set wake on lan parameters
959 * @pl: a pointer to a &struct phylink returned from phylink_create()
960 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
961 *
962 * Set the wake on lan parameters for the PHY attached to the phylink
963 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
964 * error.
965 *
966 * Returns zero on success or negative errno code.
967 */
Russell King9525ae82017-07-25 15:03:13 +0100968int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
969{
970 int ret = -EOPNOTSUPP;
971
Russell King8b874512017-12-15 16:09:47 +0000972 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100973
974 if (pl->phydev)
975 ret = phy_ethtool_set_wol(pl->phydev, wol);
976
977 return ret;
978}
979EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
980
981static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
982{
983 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
984
985 linkmode_zero(mask);
986 phylink_set_port_modes(mask);
987
988 linkmode_and(dst, dst, mask);
989 linkmode_or(dst, dst, b);
990}
991
992static void phylink_get_ksettings(const struct phylink_link_state *state,
993 struct ethtool_link_ksettings *kset)
994{
995 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
996 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
997 kset->base.speed = state->speed;
998 kset->base.duplex = state->duplex;
999 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1000 AUTONEG_DISABLE;
1001}
1002
Russell King8796c892017-12-01 10:24:47 +00001003/**
1004 * phylink_ethtool_ksettings_get() - get the current link settings
1005 * @pl: a pointer to a &struct phylink returned from phylink_create()
1006 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1007 *
1008 * Read the current link settings for the phylink instance specified by @pl.
1009 * This will be the link settings read from the MAC, PHY or fixed link
1010 * settings depending on the current negotiation mode.
1011 */
Russell King9525ae82017-07-25 15:03:13 +01001012int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001013 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001014{
1015 struct phylink_link_state link_state;
1016
Russell King8b874512017-12-15 16:09:47 +00001017 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001018
1019 if (pl->phydev) {
1020 phy_ethtool_ksettings_get(pl->phydev, kset);
1021 } else {
1022 kset->base.port = pl->link_port;
1023 }
1024
1025 linkmode_copy(kset->link_modes.supported, pl->supported);
1026
1027 switch (pl->link_an_mode) {
1028 case MLO_AN_FIXED:
1029 /* We are using fixed settings. Report these as the
1030 * current link settings - and note that these also
1031 * represent the supported speeds/duplex/pause modes.
1032 */
1033 phylink_get_fixed_state(pl, &link_state);
1034 phylink_get_ksettings(&link_state, kset);
1035 break;
1036
Russell King86a362c2017-12-01 10:24:26 +00001037 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001038 /* If there is a phy attached, then use the reported
1039 * settings from the phy with no modification.
1040 */
1041 if (pl->phydev)
1042 break;
1043
Russell King9525ae82017-07-25 15:03:13 +01001044 phylink_get_mac_state(pl, &link_state);
1045
1046 /* The MAC is reporting the link results from its own PCS
1047 * layer via in-band status. Report these as the current
1048 * link settings.
1049 */
1050 phylink_get_ksettings(&link_state, kset);
1051 break;
1052 }
1053
1054 return 0;
1055}
1056EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1057
Russell King8796c892017-12-01 10:24:47 +00001058/**
1059 * phylink_ethtool_ksettings_set() - set the link settings
1060 * @pl: a pointer to a &struct phylink returned from phylink_create()
1061 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1062 */
Russell King9525ae82017-07-25 15:03:13 +01001063int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001064 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001065{
1066 struct ethtool_link_ksettings our_kset;
1067 struct phylink_link_state config;
1068 int ret;
1069
Russell King8b874512017-12-15 16:09:47 +00001070 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001071
1072 if (kset->base.autoneg != AUTONEG_DISABLE &&
1073 kset->base.autoneg != AUTONEG_ENABLE)
1074 return -EINVAL;
1075
1076 config = pl->link_config;
1077
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001078 /* Mask out unsupported advertisements */
Russell King9525ae82017-07-25 15:03:13 +01001079 linkmode_and(config.advertising, kset->link_modes.advertising,
1080 pl->supported);
1081
1082 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1083 if (kset->base.autoneg == AUTONEG_DISABLE) {
1084 const struct phy_setting *s;
1085
1086 /* Autonegotiation disabled, select a suitable speed and
1087 * duplex.
1088 */
1089 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001090 pl->supported, false);
Russell King9525ae82017-07-25 15:03:13 +01001091 if (!s)
1092 return -EINVAL;
1093
1094 /* If we have a fixed link (as specified by firmware), refuse
1095 * to change link parameters.
1096 */
1097 if (pl->link_an_mode == MLO_AN_FIXED &&
1098 (s->speed != pl->link_config.speed ||
1099 s->duplex != pl->link_config.duplex))
1100 return -EINVAL;
1101
1102 config.speed = s->speed;
1103 config.duplex = s->duplex;
1104 config.an_enabled = false;
1105
1106 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1107 } else {
1108 /* If we have a fixed link, refuse to enable autonegotiation */
1109 if (pl->link_an_mode == MLO_AN_FIXED)
1110 return -EINVAL;
1111
1112 config.speed = SPEED_UNKNOWN;
1113 config.duplex = DUPLEX_UNKNOWN;
1114 config.an_enabled = true;
1115
1116 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1117 }
1118
1119 if (phylink_validate(pl, pl->supported, &config))
1120 return -EINVAL;
1121
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001122 /* If autonegotiation is enabled, we must have an advertisement */
Russell King9525ae82017-07-25 15:03:13 +01001123 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1124 return -EINVAL;
1125
1126 our_kset = *kset;
1127 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1128 our_kset.base.speed = config.speed;
1129 our_kset.base.duplex = config.duplex;
1130
1131 /* If we have a PHY, configure the phy */
1132 if (pl->phydev) {
1133 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1134 if (ret)
1135 return ret;
1136 }
1137
1138 mutex_lock(&pl->state_mutex);
1139 /* Configure the MAC to match the new settings */
1140 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
Russell King182088a2017-12-20 23:21:28 +00001141 pl->link_config.interface = config.interface;
Russell King9525ae82017-07-25 15:03:13 +01001142 pl->link_config.speed = our_kset.base.speed;
1143 pl->link_config.duplex = our_kset.base.duplex;
1144 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1145
1146 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1147 phylink_mac_config(pl, &pl->link_config);
1148 phylink_mac_an_restart(pl);
1149 }
1150 mutex_unlock(&pl->state_mutex);
1151
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001152 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001153}
1154EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1155
Russell King8796c892017-12-01 10:24:47 +00001156/**
1157 * phylink_ethtool_nway_reset() - restart negotiation
1158 * @pl: a pointer to a &struct phylink returned from phylink_create()
1159 *
1160 * Restart negotiation for the phylink instance specified by @pl. This will
1161 * cause any attached phy to restart negotiation with the link partner, and
1162 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1163 * negotiation.
1164 *
1165 * Returns zero on success, or negative error code.
1166 */
Russell King9525ae82017-07-25 15:03:13 +01001167int phylink_ethtool_nway_reset(struct phylink *pl)
1168{
1169 int ret = 0;
1170
Russell King8b874512017-12-15 16:09:47 +00001171 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001172
1173 if (pl->phydev)
1174 ret = phy_restart_aneg(pl->phydev);
1175 phylink_mac_an_restart(pl);
1176
1177 return ret;
1178}
1179EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1180
Russell King8796c892017-12-01 10:24:47 +00001181/**
1182 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1183 * @pl: a pointer to a &struct phylink returned from phylink_create()
1184 * @pause: a pointer to a &struct ethtool_pauseparam
1185 */
Russell King9525ae82017-07-25 15:03:13 +01001186void phylink_ethtool_get_pauseparam(struct phylink *pl,
1187 struct ethtool_pauseparam *pause)
1188{
Russell King8b874512017-12-15 16:09:47 +00001189 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001190
1191 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1192 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1193 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1194}
1195EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1196
Russell King8796c892017-12-01 10:24:47 +00001197/**
1198 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1199 * @pl: a pointer to a &struct phylink returned from phylink_create()
1200 * @pause: a pointer to a &struct ethtool_pauseparam
1201 */
Russell King9525ae82017-07-25 15:03:13 +01001202int phylink_ethtool_set_pauseparam(struct phylink *pl,
1203 struct ethtool_pauseparam *pause)
1204{
1205 struct phylink_link_state *config = &pl->link_config;
1206
Russell King8b874512017-12-15 16:09:47 +00001207 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001208
1209 if (!phylink_test(pl->supported, Pause) &&
1210 !phylink_test(pl->supported, Asym_Pause))
1211 return -EOPNOTSUPP;
1212
1213 if (!phylink_test(pl->supported, Asym_Pause) &&
1214 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1215 return -EINVAL;
1216
1217 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1218
1219 if (pause->autoneg)
1220 config->pause |= MLO_PAUSE_AN;
1221 if (pause->rx_pause)
1222 config->pause |= MLO_PAUSE_RX;
1223 if (pause->tx_pause)
1224 config->pause |= MLO_PAUSE_TX;
1225
1226 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1227 switch (pl->link_an_mode) {
1228 case MLO_AN_PHY:
1229 /* Silently mark the carrier down, and then trigger a resolve */
1230 netif_carrier_off(pl->netdev);
1231 phylink_run_resolve(pl);
1232 break;
1233
1234 case MLO_AN_FIXED:
1235 /* Should we allow fixed links to change against the config? */
1236 phylink_resolve_flow(pl, config);
1237 phylink_mac_config(pl, config);
1238 break;
1239
Russell King86a362c2017-12-01 10:24:26 +00001240 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001241 phylink_mac_config(pl, config);
1242 phylink_mac_an_restart(pl);
1243 break;
1244 }
1245 }
1246
1247 return 0;
1248}
1249EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1250
Russell King8796c892017-12-01 10:24:47 +00001251/**
1252 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1253 * counter
1254 * @pl: a pointer to a &struct phylink returned from phylink_create().
1255 *
1256 * Read the Energy Efficient Ethernet error counter from the PHY associated
1257 * with the phylink instance specified by @pl.
1258 *
1259 * Returns positive error counter value, or negative error code.
1260 */
Russell King9525ae82017-07-25 15:03:13 +01001261int phylink_get_eee_err(struct phylink *pl)
1262{
1263 int ret = 0;
1264
Russell King8b874512017-12-15 16:09:47 +00001265 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001266
1267 if (pl->phydev)
1268 ret = phy_get_eee_err(pl->phydev);
1269
1270 return ret;
1271}
1272EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1273
Russell King8796c892017-12-01 10:24:47 +00001274/**
Russell King86e58132019-02-11 11:46:06 +00001275 * phylink_init_eee() - init and check the EEE features
1276 * @pl: a pointer to a &struct phylink returned from phylink_create()
1277 * @clk_stop_enable: allow PHY to stop receive clock
1278 *
1279 * Must be called either with RTNL held or within mac_link_up()
1280 */
1281int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1282{
1283 int ret = -EOPNOTSUPP;
1284
1285 if (pl->phydev)
1286 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1287
1288 return ret;
1289}
1290EXPORT_SYMBOL_GPL(phylink_init_eee);
1291
1292/**
Russell King8796c892017-12-01 10:24:47 +00001293 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1294 * @pl: a pointer to a &struct phylink returned from phylink_create()
1295 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1296 */
Russell King9525ae82017-07-25 15:03:13 +01001297int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1298{
1299 int ret = -EOPNOTSUPP;
1300
Russell King8b874512017-12-15 16:09:47 +00001301 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001302
1303 if (pl->phydev)
1304 ret = phy_ethtool_get_eee(pl->phydev, eee);
1305
1306 return ret;
1307}
1308EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1309
Russell King8796c892017-12-01 10:24:47 +00001310/**
1311 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1312 * @pl: a pointer to a &struct phylink returned from phylink_create()
1313 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1314 */
Russell King9525ae82017-07-25 15:03:13 +01001315int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1316{
1317 int ret = -EOPNOTSUPP;
1318
Russell King8b874512017-12-15 16:09:47 +00001319 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001320
1321 if (pl->phydev)
1322 ret = phy_ethtool_set_eee(pl->phydev, eee);
1323
1324 return ret;
1325}
1326EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1327
1328/* This emulates MII registers for a fixed-mode phy operating as per the
1329 * passed in state. "aneg" defines if we report negotiation is possible.
1330 *
1331 * FIXME: should deal with negotiation state too.
1332 */
1333static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1334 struct phylink_link_state *state, bool aneg)
1335{
1336 struct fixed_phy_status fs;
1337 int val;
1338
1339 fs.link = state->link;
1340 fs.speed = state->speed;
1341 fs.duplex = state->duplex;
1342 fs.pause = state->pause & MLO_PAUSE_SYM;
1343 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1344
1345 val = swphy_read_reg(reg, &fs);
1346 if (reg == MII_BMSR) {
1347 if (!state->an_complete)
1348 val &= ~BMSR_ANEGCOMPLETE;
1349 if (!aneg)
1350 val &= ~BMSR_ANEGCAPABLE;
1351 }
1352 return val;
1353}
1354
Russell Kingecbd87b2017-07-25 15:03:28 +01001355static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1356 unsigned int reg)
1357{
1358 struct phy_device *phydev = pl->phydev;
1359 int prtad, devad;
1360
1361 if (mdio_phy_id_is_c45(phy_id)) {
1362 prtad = mdio_phy_id_prtad(phy_id);
1363 devad = mdio_phy_id_devad(phy_id);
1364 devad = MII_ADDR_C45 | devad << 16 | reg;
1365 } else if (phydev->is_c45) {
1366 switch (reg) {
1367 case MII_BMCR:
1368 case MII_BMSR:
1369 case MII_PHYSID1:
1370 case MII_PHYSID2:
1371 devad = __ffs(phydev->c45_ids.devices_in_package);
1372 break;
1373 case MII_ADVERTISE:
1374 case MII_LPA:
1375 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1376 return -EINVAL;
1377 devad = MDIO_MMD_AN;
1378 if (reg == MII_ADVERTISE)
1379 reg = MDIO_AN_ADVERTISE;
1380 else
1381 reg = MDIO_AN_LPA;
1382 break;
1383 default:
1384 return -EINVAL;
1385 }
1386 prtad = phy_id;
1387 devad = MII_ADDR_C45 | devad << 16 | reg;
1388 } else {
1389 prtad = phy_id;
1390 devad = reg;
1391 }
1392 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1393}
1394
1395static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1396 unsigned int reg, unsigned int val)
1397{
1398 struct phy_device *phydev = pl->phydev;
1399 int prtad, devad;
1400
1401 if (mdio_phy_id_is_c45(phy_id)) {
1402 prtad = mdio_phy_id_prtad(phy_id);
1403 devad = mdio_phy_id_devad(phy_id);
1404 devad = MII_ADDR_C45 | devad << 16 | reg;
1405 } else if (phydev->is_c45) {
1406 switch (reg) {
1407 case MII_BMCR:
1408 case MII_BMSR:
1409 case MII_PHYSID1:
1410 case MII_PHYSID2:
1411 devad = __ffs(phydev->c45_ids.devices_in_package);
1412 break;
1413 case MII_ADVERTISE:
1414 case MII_LPA:
1415 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1416 return -EINVAL;
1417 devad = MDIO_MMD_AN;
1418 if (reg == MII_ADVERTISE)
1419 reg = MDIO_AN_ADVERTISE;
1420 else
1421 reg = MDIO_AN_LPA;
1422 break;
1423 default:
1424 return -EINVAL;
1425 }
1426 prtad = phy_id;
1427 devad = MII_ADDR_C45 | devad << 16 | reg;
1428 } else {
1429 prtad = phy_id;
1430 devad = reg;
1431 }
1432
1433 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1434}
1435
Russell King9525ae82017-07-25 15:03:13 +01001436static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1437 unsigned int reg)
1438{
1439 struct phylink_link_state state;
1440 int val = 0xffff;
1441
Russell King9525ae82017-07-25 15:03:13 +01001442 switch (pl->link_an_mode) {
1443 case MLO_AN_FIXED:
1444 if (phy_id == 0) {
1445 phylink_get_fixed_state(pl, &state);
1446 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1447 true);
1448 }
1449 break;
1450
1451 case MLO_AN_PHY:
1452 return -EOPNOTSUPP;
1453
Russell King86a362c2017-12-01 10:24:26 +00001454 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001455 if (phy_id == 0) {
1456 val = phylink_get_mac_state(pl, &state);
1457 if (val < 0)
1458 return val;
1459
1460 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1461 true);
1462 }
1463 break;
1464 }
1465
1466 return val & 0xffff;
1467}
1468
1469static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1470 unsigned int reg, unsigned int val)
1471{
Russell King9525ae82017-07-25 15:03:13 +01001472 switch (pl->link_an_mode) {
1473 case MLO_AN_FIXED:
1474 break;
1475
1476 case MLO_AN_PHY:
1477 return -EOPNOTSUPP;
1478
Russell King86a362c2017-12-01 10:24:26 +00001479 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001480 break;
1481 }
1482
1483 return 0;
1484}
1485
Russell King8796c892017-12-01 10:24:47 +00001486/**
1487 * phylink_mii_ioctl() - generic mii ioctl interface
1488 * @pl: a pointer to a &struct phylink returned from phylink_create()
1489 * @ifr: a pointer to a &struct ifreq for socket ioctls
1490 * @cmd: ioctl cmd to execute
1491 *
1492 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1493 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1494 *
1495 * Returns: zero on success or negative error code.
1496 *
1497 * %SIOCGMIIPHY:
1498 * read register from the current PHY.
1499 * %SIOCGMIIREG:
1500 * read register from the specified PHY.
1501 * %SIOCSMIIREG:
1502 * set a register on the specified PHY.
1503 */
Russell King9525ae82017-07-25 15:03:13 +01001504int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1505{
Russell Kingecbd87b2017-07-25 15:03:28 +01001506 struct mii_ioctl_data *mii = if_mii(ifr);
1507 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001508
Russell King8b874512017-12-15 16:09:47 +00001509 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001510
Russell Kingecbd87b2017-07-25 15:03:28 +01001511 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001512 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001513 switch (cmd) {
1514 case SIOCGMIIPHY:
1515 mii->phy_id = pl->phydev->mdio.addr;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001516 /* fall through */
Russell King9525ae82017-07-25 15:03:13 +01001517
Russell Kingecbd87b2017-07-25 15:03:28 +01001518 case SIOCGMIIREG:
1519 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1520 if (ret >= 0) {
1521 mii->val_out = ret;
1522 ret = 0;
1523 }
1524 break;
Russell King9525ae82017-07-25 15:03:13 +01001525
Russell Kingecbd87b2017-07-25 15:03:28 +01001526 case SIOCSMIIREG:
1527 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1528 mii->val_in);
1529 break;
Russell King9525ae82017-07-25 15:03:13 +01001530
Russell Kingecbd87b2017-07-25 15:03:28 +01001531 default:
Russell King9525ae82017-07-25 15:03:13 +01001532 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001533 break;
1534 }
1535 } else {
1536 switch (cmd) {
1537 case SIOCGMIIPHY:
1538 mii->phy_id = 0;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001539 /* fall through */
Russell Kingecbd87b2017-07-25 15:03:28 +01001540
1541 case SIOCGMIIREG:
1542 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1543 if (ret >= 0) {
1544 mii->val_out = ret;
1545 ret = 0;
1546 }
1547 break;
1548
1549 case SIOCSMIIREG:
1550 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1551 mii->val_in);
1552 break;
1553
1554 default:
1555 ret = -EOPNOTSUPP;
1556 break;
1557 }
Russell King9525ae82017-07-25 15:03:13 +01001558 }
1559
1560 return ret;
1561}
1562EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1563
Russell Kingce0aa272017-07-25 15:03:18 +01001564static int phylink_sfp_module_insert(void *upstream,
1565 const struct sfp_eeprom_id *id)
1566{
1567 struct phylink *pl = upstream;
1568 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1569 struct phylink_link_state config;
1570 phy_interface_t iface;
Russell King444d3502017-12-29 12:15:33 +00001571 int ret = 0;
Russell Kingce0aa272017-07-25 15:03:18 +01001572 bool changed;
1573 u8 port;
1574
Russell King8b874512017-12-15 16:09:47 +00001575 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001576
Russell Kinga9c79362018-02-27 15:53:02 +00001577 sfp_parse_support(pl->sfp_bus, id, support);
1578 port = sfp_parse_port(pl->sfp_bus, id, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001579
1580 memset(&config, 0, sizeof(config));
1581 linkmode_copy(config.advertising, support);
Russell Kinga9c79362018-02-27 15:53:02 +00001582 config.interface = PHY_INTERFACE_MODE_NA;
Russell Kingce0aa272017-07-25 15:03:18 +01001583 config.speed = SPEED_UNKNOWN;
1584 config.duplex = DUPLEX_UNKNOWN;
1585 config.pause = MLO_PAUSE_AN;
1586 config.an_enabled = pl->link_config.an_enabled;
1587
1588 /* Ignore errors if we're expecting a PHY to attach later */
1589 ret = phylink_validate(pl, support, &config);
1590 if (ret) {
Russell Kinga9c79362018-02-27 15:53:02 +00001591 netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
1592 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1593 return ret;
1594 }
1595
1596 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1597 if (iface == PHY_INTERFACE_MODE_NA) {
1598 netdev_err(pl->netdev,
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001599 "selection of interface failed, advertisement %*pb\n",
Russell Kinga9c79362018-02-27 15:53:02 +00001600 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1601 return -EINVAL;
1602 }
1603
1604 config.interface = iface;
1605 ret = phylink_validate(pl, support, &config);
1606 if (ret) {
Russell Kingce0aa272017-07-25 15:03:18 +01001607 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
Russell King444d3502017-12-29 12:15:33 +00001608 phylink_an_mode_str(MLO_AN_INBAND),
1609 phy_modes(config.interface),
Russell Kingce0aa272017-07-25 15:03:18 +01001610 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1611 return ret;
1612 }
1613
1614 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
Russell King444d3502017-12-29 12:15:33 +00001615 phylink_an_mode_str(MLO_AN_INBAND),
1616 phy_modes(config.interface),
Russell Kingce0aa272017-07-25 15:03:18 +01001617 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1618
Russell King86a362c2017-12-01 10:24:26 +00001619 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001620 return -EINVAL;
1621
1622 changed = !bitmap_equal(pl->supported, support,
1623 __ETHTOOL_LINK_MODE_MASK_NBITS);
1624 if (changed) {
1625 linkmode_copy(pl->supported, support);
1626 linkmode_copy(pl->link_config.advertising, config.advertising);
1627 }
1628
Russell King444d3502017-12-29 12:15:33 +00001629 if (pl->link_an_mode != MLO_AN_INBAND ||
Russell Kingce0aa272017-07-25 15:03:18 +01001630 pl->link_config.interface != config.interface) {
1631 pl->link_config.interface = config.interface;
Russell King444d3502017-12-29 12:15:33 +00001632 pl->link_an_mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001633
1634 changed = true;
1635
1636 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
Russell King444d3502017-12-29 12:15:33 +00001637 phylink_an_mode_str(MLO_AN_INBAND),
Russell Kingce0aa272017-07-25 15:03:18 +01001638 phy_modes(config.interface));
1639 }
1640
1641 pl->link_port = port;
1642
1643 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1644 &pl->phylink_disable_state))
1645 phylink_mac_config(pl, &pl->link_config);
1646
1647 return ret;
1648}
1649
1650static void phylink_sfp_link_down(void *upstream)
1651{
1652 struct phylink *pl = upstream;
1653
Russell King8b874512017-12-15 16:09:47 +00001654 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001655
1656 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
Russell Kingac817f5a2017-12-26 23:15:12 +00001657 queue_work(system_power_efficient_wq, &pl->resolve);
Russell Kingce0aa272017-07-25 15:03:18 +01001658 flush_work(&pl->resolve);
Russell Kingce0aa272017-07-25 15:03:18 +01001659}
1660
1661static void phylink_sfp_link_up(void *upstream)
1662{
1663 struct phylink *pl = upstream;
1664
Russell King8b874512017-12-15 16:09:47 +00001665 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001666
1667 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1668 phylink_run_resolve(pl);
1669}
1670
1671static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1672{
Baruch Siach7e418372018-10-03 19:04:49 +03001673 struct phylink *pl = upstream;
1674
1675 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
Russell Kingce0aa272017-07-25 15:03:18 +01001676}
1677
1678static void phylink_sfp_disconnect_phy(void *upstream)
1679{
1680 phylink_disconnect_phy(upstream);
1681}
1682
1683static const struct sfp_upstream_ops sfp_phylink_ops = {
1684 .module_insert = phylink_sfp_module_insert,
1685 .link_up = phylink_sfp_link_up,
1686 .link_down = phylink_sfp_link_down,
1687 .connect_phy = phylink_sfp_connect_phy,
1688 .disconnect_phy = phylink_sfp_disconnect_phy,
1689};
1690
Russell King624c0f02018-08-09 15:38:38 +02001691/* Helpers for MAC drivers */
1692
1693/**
1694 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1695 * @state: a pointer to a &struct phylink_link_state
1696 *
1697 * Inspect the interface mode, advertising mask or forced speed and
1698 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1699 * the interface mode to suit. @state->interface is appropriately
1700 * updated, and the advertising mask has the "other" baseX_Full flag
1701 * cleared.
1702 */
1703void phylink_helper_basex_speed(struct phylink_link_state *state)
1704{
1705 if (phy_interface_mode_is_8023z(state->interface)) {
1706 bool want_2500 = state->an_enabled ?
1707 phylink_test(state->advertising, 2500baseX_Full) :
1708 state->speed == SPEED_2500;
1709
1710 if (want_2500) {
1711 phylink_clear(state->advertising, 1000baseX_Full);
1712 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1713 } else {
1714 phylink_clear(state->advertising, 2500baseX_Full);
1715 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1716 }
1717 }
1718}
1719EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1720
Andrew Lunn5f857572019-01-21 19:10:19 +01001721MODULE_LICENSE("GPL v2");