blob: 40563c8d73521dc41df46762d822bdf6473ca12a [file] [log] [blame]
Russell King9525ae82017-07-25 15:03:13 +01001/*
2 * phylink models the MAC to optional PHY connection, supporting
3 * technologies such as SFP cages where the PHY is hot-pluggable.
4 *
5 * Copyright (C) 2015 Russell King
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/ethtool.h>
12#include <linux/export.h>
13#include <linux/gpio/consumer.h>
14#include <linux/netdevice.h>
15#include <linux/of.h>
16#include <linux/of_mdio.h>
17#include <linux/phy.h>
18#include <linux/phy_fixed.h>
19#include <linux/phylink.h>
20#include <linux/rtnetlink.h>
21#include <linux/spinlock.h>
22#include <linux/workqueue.h>
23
Russell Kingce0aa272017-07-25 15:03:18 +010024#include "sfp.h"
Russell King9525ae82017-07-25 15:03:13 +010025#include "swphy.h"
26
27#define SUPPORTED_INTERFACES \
28 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
29 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
30#define ADVERTISED_INTERFACES \
31 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
32 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
33
34enum {
35 PHYLINK_DISABLE_STOPPED,
Russell Kingce0aa272017-07-25 15:03:18 +010036 PHYLINK_DISABLE_LINK,
Russell King9525ae82017-07-25 15:03:13 +010037};
38
39struct phylink {
40 struct net_device *netdev;
41 const struct phylink_mac_ops *ops;
42
43 unsigned long phylink_disable_state; /* bitmask of disables */
44 struct phy_device *phydev;
45 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
46 u8 link_an_mode; /* MLO_AN_xxx */
47 u8 link_port; /* The current non-phy ethtool port */
48 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
49
50 /* The link configuration settings */
51 struct phylink_link_state link_config;
52 struct gpio_desc *link_gpio;
53
54 struct mutex state_mutex;
55 struct phylink_link_state phy_state;
56 struct work_struct resolve;
57
58 bool mac_link_dropped;
Russell Kingce0aa272017-07-25 15:03:18 +010059
60 struct sfp_bus *sfp_bus;
Russell King9525ae82017-07-25 15:03:13 +010061};
62
63static inline void linkmode_zero(unsigned long *dst)
64{
65 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
66}
67
68static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
69{
70 bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
71}
72
73static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
74 const unsigned long *b)
75{
76 bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
77}
78
79static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
80 const unsigned long *b)
81{
82 bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
83}
84
85static inline bool linkmode_empty(const unsigned long *src)
86{
87 return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
88}
89
90void phylink_set_port_modes(unsigned long *mask)
91{
92 phylink_set(mask, TP);
93 phylink_set(mask, AUI);
94 phylink_set(mask, MII);
95 phylink_set(mask, FIBRE);
96 phylink_set(mask, BNC);
97 phylink_set(mask, Backplane);
98}
99EXPORT_SYMBOL_GPL(phylink_set_port_modes);
100
101static int phylink_is_empty_linkmode(const unsigned long *linkmode)
102{
103 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
104
105 phylink_set_port_modes(tmp);
106 phylink_set(tmp, Autoneg);
107 phylink_set(tmp, Pause);
108 phylink_set(tmp, Asym_Pause);
109
110 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
111
112 return linkmode_empty(tmp);
113}
114
115static const char *phylink_an_mode_str(unsigned int mode)
116{
117 static const char *modestr[] = {
118 [MLO_AN_PHY] = "phy",
119 [MLO_AN_FIXED] = "fixed",
Russell King86a362c2017-12-01 10:24:26 +0000120 [MLO_AN_INBAND] = "inband",
Russell King9525ae82017-07-25 15:03:13 +0100121 };
122
123 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
124}
125
126static int phylink_validate(struct phylink *pl, unsigned long *supported,
127 struct phylink_link_state *state)
128{
129 pl->ops->validate(pl->netdev, supported, state);
130
131 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
132}
133
134static int phylink_parse_fixedlink(struct phylink *pl, struct device_node *np)
135{
136 struct device_node *fixed_node;
137 const struct phy_setting *s;
138 struct gpio_desc *desc;
139 const __be32 *fixed_prop;
140 u32 speed;
141 int ret, len;
142
143 fixed_node = of_get_child_by_name(np, "fixed-link");
144 if (fixed_node) {
145 ret = of_property_read_u32(fixed_node, "speed", &speed);
146
147 pl->link_config.speed = speed;
148 pl->link_config.duplex = DUPLEX_HALF;
149
150 if (of_property_read_bool(fixed_node, "full-duplex"))
151 pl->link_config.duplex = DUPLEX_FULL;
152
153 /* We treat the "pause" and "asym-pause" terminology as
154 * defining the link partner's ability. */
155 if (of_property_read_bool(fixed_node, "pause"))
156 pl->link_config.pause |= MLO_PAUSE_SYM;
157 if (of_property_read_bool(fixed_node, "asym-pause"))
158 pl->link_config.pause |= MLO_PAUSE_ASYM;
159
160 if (ret == 0) {
161 desc = fwnode_get_named_gpiod(&fixed_node->fwnode,
162 "link-gpios", 0,
163 GPIOD_IN, "?");
164
165 if (!IS_ERR(desc))
166 pl->link_gpio = desc;
167 else if (desc == ERR_PTR(-EPROBE_DEFER))
168 ret = -EPROBE_DEFER;
169 }
170 of_node_put(fixed_node);
171
172 if (ret)
173 return ret;
174 } else {
175 fixed_prop = of_get_property(np, "fixed-link", &len);
176 if (!fixed_prop) {
177 netdev_err(pl->netdev, "broken fixed-link?\n");
178 return -EINVAL;
179 }
180 if (len == 5 * sizeof(*fixed_prop)) {
181 pl->link_config.duplex = be32_to_cpu(fixed_prop[1]) ?
182 DUPLEX_FULL : DUPLEX_HALF;
183 pl->link_config.speed = be32_to_cpu(fixed_prop[2]);
184 if (be32_to_cpu(fixed_prop[3]))
185 pl->link_config.pause |= MLO_PAUSE_SYM;
186 if (be32_to_cpu(fixed_prop[4]))
187 pl->link_config.pause |= MLO_PAUSE_ASYM;
188 }
189 }
190
191 if (pl->link_config.speed > SPEED_1000 &&
192 pl->link_config.duplex != DUPLEX_FULL)
193 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
194 pl->link_config.speed);
195
196 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
197 linkmode_copy(pl->link_config.advertising, pl->supported);
198 phylink_validate(pl, pl->supported, &pl->link_config);
199
200 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
201 pl->supported,
202 __ETHTOOL_LINK_MODE_MASK_NBITS, true);
203 linkmode_zero(pl->supported);
204 phylink_set(pl->supported, MII);
205 if (s) {
206 __set_bit(s->bit, pl->supported);
207 } else {
208 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
209 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
210 pl->link_config.speed);
211 }
212
213 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
214 pl->supported);
215
216 pl->link_config.link = 1;
217 pl->link_config.an_complete = 1;
218
219 return 0;
220}
221
222static int phylink_parse_mode(struct phylink *pl, struct device_node *np)
223{
224 struct device_node *dn;
225 const char *managed;
226
227 dn = of_get_child_by_name(np, "fixed-link");
228 if (dn || of_find_property(np, "fixed-link", NULL))
229 pl->link_an_mode = MLO_AN_FIXED;
230 of_node_put(dn);
231
232 if (of_property_read_string(np, "managed", &managed) == 0 &&
233 strcmp(managed, "in-band-status") == 0) {
234 if (pl->link_an_mode == MLO_AN_FIXED) {
235 netdev_err(pl->netdev,
236 "can't use both fixed-link and in-band-status\n");
237 return -EINVAL;
238 }
239
240 linkmode_zero(pl->supported);
241 phylink_set(pl->supported, MII);
242 phylink_set(pl->supported, Autoneg);
243 phylink_set(pl->supported, Asym_Pause);
244 phylink_set(pl->supported, Pause);
245 pl->link_config.an_enabled = true;
Russell King86a362c2017-12-01 10:24:26 +0000246 pl->link_an_mode = MLO_AN_INBAND;
Russell King9525ae82017-07-25 15:03:13 +0100247
248 switch (pl->link_config.interface) {
249 case PHY_INTERFACE_MODE_SGMII:
250 phylink_set(pl->supported, 10baseT_Half);
251 phylink_set(pl->supported, 10baseT_Full);
252 phylink_set(pl->supported, 100baseT_Half);
253 phylink_set(pl->supported, 100baseT_Full);
254 phylink_set(pl->supported, 1000baseT_Half);
255 phylink_set(pl->supported, 1000baseT_Full);
Russell King9525ae82017-07-25 15:03:13 +0100256 break;
257
258 case PHY_INTERFACE_MODE_1000BASEX:
259 phylink_set(pl->supported, 1000baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100260 break;
261
262 case PHY_INTERFACE_MODE_2500BASEX:
263 phylink_set(pl->supported, 2500baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100264 break;
265
Russell Kingda7c1862017-07-25 15:03:34 +0100266 case PHY_INTERFACE_MODE_10GKR:
267 phylink_set(pl->supported, 10baseT_Half);
268 phylink_set(pl->supported, 10baseT_Full);
269 phylink_set(pl->supported, 100baseT_Half);
270 phylink_set(pl->supported, 100baseT_Full);
271 phylink_set(pl->supported, 1000baseT_Half);
272 phylink_set(pl->supported, 1000baseT_Full);
273 phylink_set(pl->supported, 1000baseX_Full);
274 phylink_set(pl->supported, 10000baseKR_Full);
275 phylink_set(pl->supported, 10000baseCR_Full);
276 phylink_set(pl->supported, 10000baseSR_Full);
277 phylink_set(pl->supported, 10000baseLR_Full);
278 phylink_set(pl->supported, 10000baseLRM_Full);
279 phylink_set(pl->supported, 10000baseER_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100280 break;
281
Russell King9525ae82017-07-25 15:03:13 +0100282 default:
283 netdev_err(pl->netdev,
284 "incorrect link mode %s for in-band status\n",
285 phy_modes(pl->link_config.interface));
286 return -EINVAL;
287 }
288
289 linkmode_copy(pl->link_config.advertising, pl->supported);
290
291 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
292 netdev_err(pl->netdev,
293 "failed to validate link configuration for in-band status\n");
294 return -EINVAL;
295 }
296 }
297
298 return 0;
299}
300
301static void phylink_mac_config(struct phylink *pl,
302 const struct phylink_link_state *state)
303{
304 netdev_dbg(pl->netdev,
305 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
306 __func__, phylink_an_mode_str(pl->link_an_mode),
307 phy_modes(state->interface),
308 phy_speed_to_str(state->speed),
309 phy_duplex_to_str(state->duplex),
310 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
311 state->pause, state->link, state->an_enabled);
312
313 pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
314}
315
316static void phylink_mac_an_restart(struct phylink *pl)
317{
318 if (pl->link_config.an_enabled &&
Russell King365c1e62017-12-01 10:24:16 +0000319 phy_interface_mode_is_8023z(pl->link_config.interface))
Russell King9525ae82017-07-25 15:03:13 +0100320 pl->ops->mac_an_restart(pl->netdev);
321}
322
323static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
324{
325 struct net_device *ndev = pl->netdev;
326
327 linkmode_copy(state->advertising, pl->link_config.advertising);
328 linkmode_zero(state->lp_advertising);
329 state->interface = pl->link_config.interface;
330 state->an_enabled = pl->link_config.an_enabled;
331 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,
337 * which may be determined by a GPIO.
338 */
339static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
340{
341 *state = pl->link_config;
342 if (pl->link_gpio)
343 state->link = !!gpiod_get_value(pl->link_gpio);
344}
345
346/* Flow control is resolved according to our and the link partners
347 * advertisments using the following drawn from the 802.3 specs:
348 * Local device Link partner
349 * Pause AsymDir Pause AsymDir Result
350 * 1 X 1 X TX+RX
351 * 0 1 1 1 RX
352 * 1 1 0 1 TX
353 */
354static void phylink_resolve_flow(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700355 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100356{
357 int new_pause = 0;
358
359 if (pl->link_config.pause & MLO_PAUSE_AN) {
360 int pause = 0;
361
362 if (phylink_test(pl->link_config.advertising, Pause))
363 pause |= MLO_PAUSE_SYM;
364 if (phylink_test(pl->link_config.advertising, Asym_Pause))
365 pause |= MLO_PAUSE_ASYM;
366
367 pause &= state->pause;
368
369 if (pause & MLO_PAUSE_SYM)
370 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
371 else if (pause & MLO_PAUSE_ASYM)
372 new_pause = state->pause & MLO_PAUSE_SYM ?
373 MLO_PAUSE_RX : MLO_PAUSE_TX;
374 } else {
375 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
376 }
377
378 state->pause &= ~MLO_PAUSE_TXRX_MASK;
379 state->pause |= new_pause;
380}
381
382static const char *phylink_pause_to_str(int pause)
383{
384 switch (pause & MLO_PAUSE_TXRX_MASK) {
385 case MLO_PAUSE_TX | MLO_PAUSE_RX:
386 return "rx/tx";
387 case MLO_PAUSE_TX:
388 return "tx";
389 case MLO_PAUSE_RX:
390 return "rx";
391 default:
392 return "off";
393 }
394}
395
396static void phylink_resolve(struct work_struct *w)
397{
398 struct phylink *pl = container_of(w, struct phylink, resolve);
399 struct phylink_link_state link_state;
400 struct net_device *ndev = pl->netdev;
401
402 mutex_lock(&pl->state_mutex);
403 if (pl->phylink_disable_state) {
404 pl->mac_link_dropped = false;
405 link_state.link = false;
406 } else if (pl->mac_link_dropped) {
407 link_state.link = false;
408 } else {
409 switch (pl->link_an_mode) {
410 case MLO_AN_PHY:
411 link_state = pl->phy_state;
412 phylink_resolve_flow(pl, &link_state);
413 phylink_mac_config(pl, &link_state);
414 break;
415
416 case MLO_AN_FIXED:
417 phylink_get_fixed_state(pl, &link_state);
418 phylink_mac_config(pl, &link_state);
419 break;
420
Russell King86a362c2017-12-01 10:24:26 +0000421 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100422 phylink_get_mac_state(pl, &link_state);
423 if (pl->phydev) {
424 bool changed = false;
425
426 link_state.link = link_state.link &&
427 pl->phy_state.link;
428
429 if (pl->phy_state.interface !=
430 link_state.interface) {
431 link_state.interface = pl->phy_state.interface;
432 changed = true;
433 }
434
435 /* Propagate the flow control from the PHY
436 * to the MAC. Also propagate the interface
437 * if changed.
438 */
439 if (pl->phy_state.link || changed) {
440 link_state.pause |= pl->phy_state.pause;
441 phylink_resolve_flow(pl, &link_state);
442
443 phylink_mac_config(pl, &link_state);
444 }
445 }
446 break;
Russell King9525ae82017-07-25 15:03:13 +0100447 }
448 }
449
450 if (link_state.link != netif_carrier_ok(ndev)) {
451 if (!link_state.link) {
452 netif_carrier_off(ndev);
453 pl->ops->mac_link_down(ndev, pl->link_an_mode);
454 netdev_info(ndev, "Link is Down\n");
455 } else {
456 pl->ops->mac_link_up(ndev, pl->link_an_mode,
457 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 Kingce0aa272017-07-25 15:03:18 +0100481static const struct sfp_upstream_ops sfp_phylink_ops;
482
483static int phylink_register_sfp(struct phylink *pl, struct device_node *np)
484{
485 struct device_node *sfp_np;
486
487 sfp_np = of_parse_phandle(np, "sfp", 0);
488 if (!sfp_np)
489 return 0;
490
491 pl->sfp_bus = sfp_register_upstream(sfp_np, pl->netdev, pl,
492 &sfp_phylink_ops);
493 if (!pl->sfp_bus)
494 return -ENOMEM;
495
496 return 0;
497}
498
Russell King9525ae82017-07-25 15:03:13 +0100499struct phylink *phylink_create(struct net_device *ndev, struct device_node *np,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700500 phy_interface_t iface,
501 const struct phylink_mac_ops *ops)
Russell King9525ae82017-07-25 15:03:13 +0100502{
503 struct phylink *pl;
504 int ret;
505
506 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
507 if (!pl)
508 return ERR_PTR(-ENOMEM);
509
510 mutex_init(&pl->state_mutex);
511 INIT_WORK(&pl->resolve, phylink_resolve);
512 pl->netdev = ndev;
513 pl->phy_state.interface = iface;
514 pl->link_interface = iface;
515 pl->link_port = PORT_MII;
516 pl->link_config.interface = iface;
517 pl->link_config.pause = MLO_PAUSE_AN;
518 pl->link_config.speed = SPEED_UNKNOWN;
519 pl->link_config.duplex = DUPLEX_UNKNOWN;
520 pl->ops = ops;
521 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
522
523 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
524 linkmode_copy(pl->link_config.advertising, pl->supported);
525 phylink_validate(pl, pl->supported, &pl->link_config);
526
527 ret = phylink_parse_mode(pl, np);
528 if (ret < 0) {
529 kfree(pl);
530 return ERR_PTR(ret);
531 }
532
533 if (pl->link_an_mode == MLO_AN_FIXED) {
534 ret = phylink_parse_fixedlink(pl, np);
535 if (ret < 0) {
536 kfree(pl);
537 return ERR_PTR(ret);
538 }
539 }
540
Russell Kingce0aa272017-07-25 15:03:18 +0100541 ret = phylink_register_sfp(pl, np);
542 if (ret < 0) {
543 kfree(pl);
544 return ERR_PTR(ret);
545 }
546
Russell King9525ae82017-07-25 15:03:13 +0100547 return pl;
548}
549EXPORT_SYMBOL_GPL(phylink_create);
550
551void phylink_destroy(struct phylink *pl)
552{
Russell Kingce0aa272017-07-25 15:03:18 +0100553 if (pl->sfp_bus)
554 sfp_unregister_upstream(pl->sfp_bus);
555
Russell King9525ae82017-07-25 15:03:13 +0100556 cancel_work_sync(&pl->resolve);
557 kfree(pl);
558}
559EXPORT_SYMBOL_GPL(phylink_destroy);
560
Wei Yongjun1ec6e532017-11-02 11:14:48 +0000561static void phylink_phy_change(struct phy_device *phydev, bool up,
562 bool do_carrier)
Russell King9525ae82017-07-25 15:03:13 +0100563{
564 struct phylink *pl = phydev->phylink;
565
566 mutex_lock(&pl->state_mutex);
567 pl->phy_state.speed = phydev->speed;
568 pl->phy_state.duplex = phydev->duplex;
569 pl->phy_state.pause = MLO_PAUSE_NONE;
570 if (phydev->pause)
571 pl->phy_state.pause |= MLO_PAUSE_SYM;
572 if (phydev->asym_pause)
573 pl->phy_state.pause |= MLO_PAUSE_ASYM;
574 pl->phy_state.interface = phydev->interface;
575 pl->phy_state.link = up;
576 mutex_unlock(&pl->state_mutex);
577
578 phylink_run_resolve(pl);
579
580 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
Florian Fainelli516b29e2017-10-30 21:42:57 -0700581 phy_modes(phydev->interface),
Russell King9525ae82017-07-25 15:03:13 +0100582 phy_speed_to_str(phydev->speed),
583 phy_duplex_to_str(phydev->duplex));
584}
585
586static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
587{
588 struct phylink_link_state config;
589 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
590 u32 advertising;
591 int ret;
592
593 memset(&config, 0, sizeof(config));
594 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
595 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
596 phy->advertising);
597 config.interface = pl->link_config.interface;
598
599 /*
600 * This is the new way of dealing with flow control for PHYs,
601 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
602 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
603 * using our validate call to the MAC, we rely upon the MAC
604 * clearing the bits from both supported and advertising fields.
605 */
606 if (phylink_test(supported, Pause))
607 phylink_set(config.advertising, Pause);
608 if (phylink_test(supported, Asym_Pause))
609 phylink_set(config.advertising, Asym_Pause);
610
611 ret = phylink_validate(pl, supported, &config);
612 if (ret)
613 return ret;
614
615 phy->phylink = pl;
616 phy->phy_link_change = phylink_phy_change;
617
618 netdev_info(pl->netdev,
619 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
620 phy->drv->name);
621
622 mutex_lock(&phy->lock);
623 mutex_lock(&pl->state_mutex);
624 pl->netdev->phydev = phy;
625 pl->phydev = phy;
626 linkmode_copy(pl->supported, supported);
627 linkmode_copy(pl->link_config.advertising, config.advertising);
628
629 /* Restrict the phy advertisment according to the MAC support. */
630 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
631 phy->advertising = advertising;
632 mutex_unlock(&pl->state_mutex);
633 mutex_unlock(&phy->lock);
634
635 netdev_dbg(pl->netdev,
636 "phy: setting supported %*pb advertising 0x%08x\n",
637 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
638 phy->advertising);
639
640 phy_start_machine(phy);
641 if (phy->irq > 0)
642 phy_start_interrupts(phy);
643
644 return 0;
645}
646
647int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
648{
649 int ret;
650
Russell Kingcf4f2672017-12-01 10:24:21 +0000651 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000652 (pl->link_an_mode == MLO_AN_INBAND &&
653 phy_interface_mode_is_8023z(pl->link_interface))))
Russell Kingcf4f2672017-12-01 10:24:21 +0000654 return -EINVAL;
655
Russell King9525ae82017-07-25 15:03:13 +0100656 ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
657 if (ret)
658 return ret;
659
660 ret = phylink_bringup_phy(pl, phy);
661 if (ret)
662 phy_detach(phy);
663
664 return ret;
665}
666EXPORT_SYMBOL_GPL(phylink_connect_phy);
667
668int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn)
669{
670 struct device_node *phy_node;
671 struct phy_device *phy_dev;
672 int ret;
673
Russell Kingcf4f2672017-12-01 10:24:21 +0000674 /* Fixed links and 802.3z are handled without needing a PHY */
675 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000676 (pl->link_an_mode == MLO_AN_INBAND &&
677 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100678 return 0;
679
680 phy_node = of_parse_phandle(dn, "phy-handle", 0);
681 if (!phy_node)
682 phy_node = of_parse_phandle(dn, "phy", 0);
683 if (!phy_node)
684 phy_node = of_parse_phandle(dn, "phy-device", 0);
685
686 if (!phy_node) {
687 if (pl->link_an_mode == MLO_AN_PHY) {
688 netdev_err(pl->netdev, "unable to find PHY node\n");
689 return -ENODEV;
690 }
691 return 0;
692 }
693
694 phy_dev = of_phy_attach(pl->netdev, phy_node, 0, pl->link_interface);
695 /* We're done with the phy_node handle */
696 of_node_put(phy_node);
697
698 if (!phy_dev)
699 return -ENODEV;
700
701 ret = phylink_bringup_phy(pl, phy_dev);
702 if (ret)
703 phy_detach(phy_dev);
704
705 return ret;
706}
707EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
708
709void phylink_disconnect_phy(struct phylink *pl)
710{
711 struct phy_device *phy;
712
713 WARN_ON(!lockdep_rtnl_is_held());
714
715 phy = pl->phydev;
716 if (phy) {
717 mutex_lock(&phy->lock);
718 mutex_lock(&pl->state_mutex);
719 pl->netdev->phydev = NULL;
720 pl->phydev = NULL;
721 mutex_unlock(&pl->state_mutex);
722 mutex_unlock(&phy->lock);
723 flush_work(&pl->resolve);
724
725 phy_disconnect(phy);
726 }
727}
728EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
729
730void phylink_mac_change(struct phylink *pl, bool up)
731{
732 if (!up)
733 pl->mac_link_dropped = true;
734 phylink_run_resolve(pl);
735 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
736}
737EXPORT_SYMBOL_GPL(phylink_mac_change);
738
739void phylink_start(struct phylink *pl)
740{
741 WARN_ON(!lockdep_rtnl_is_held());
742
743 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
744 phylink_an_mode_str(pl->link_an_mode),
745 phy_modes(pl->link_config.interface));
746
747 /* Apply the link configuration to the MAC when starting. This allows
748 * a fixed-link to start with the correct parameters, and also
749 * ensures that we set the appropriate advertisment for Serdes links.
750 */
751 phylink_resolve_flow(pl, &pl->link_config);
752 phylink_mac_config(pl, &pl->link_config);
753
Russell King85b43942017-12-01 10:24:42 +0000754 /* Restart autonegotiation if using 802.3z to ensure that the link
755 * parameters are properly negotiated. This is necessary for DSA
756 * switches using 802.3z negotiation to ensure they see our modes.
757 */
758 phylink_mac_an_restart(pl);
759
Russell King9525ae82017-07-25 15:03:13 +0100760 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
761 phylink_run_resolve(pl);
762
Russell Kingce0aa272017-07-25 15:03:18 +0100763 if (pl->sfp_bus)
764 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100765 if (pl->phydev)
766 phy_start(pl->phydev);
767}
768EXPORT_SYMBOL_GPL(phylink_start);
769
770void phylink_stop(struct phylink *pl)
771{
772 WARN_ON(!lockdep_rtnl_is_held());
773
774 if (pl->phydev)
775 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100776 if (pl->sfp_bus)
777 sfp_upstream_stop(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100778
779 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King2012b7d2017-11-30 13:59:26 +0000780 queue_work(system_power_efficient_wq, &pl->resolve);
Russell King9525ae82017-07-25 15:03:13 +0100781 flush_work(&pl->resolve);
782}
783EXPORT_SYMBOL_GPL(phylink_stop);
784
785void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
786{
787 WARN_ON(!lockdep_rtnl_is_held());
788
789 wol->supported = 0;
790 wol->wolopts = 0;
791
792 if (pl->phydev)
793 phy_ethtool_get_wol(pl->phydev, wol);
794}
795EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
796
797int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
798{
799 int ret = -EOPNOTSUPP;
800
801 WARN_ON(!lockdep_rtnl_is_held());
802
803 if (pl->phydev)
804 ret = phy_ethtool_set_wol(pl->phydev, wol);
805
806 return ret;
807}
808EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
809
810static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
811{
812 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
813
814 linkmode_zero(mask);
815 phylink_set_port_modes(mask);
816
817 linkmode_and(dst, dst, mask);
818 linkmode_or(dst, dst, b);
819}
820
821static void phylink_get_ksettings(const struct phylink_link_state *state,
822 struct ethtool_link_ksettings *kset)
823{
824 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
825 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
826 kset->base.speed = state->speed;
827 kset->base.duplex = state->duplex;
828 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
829 AUTONEG_DISABLE;
830}
831
832int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700833 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +0100834{
835 struct phylink_link_state link_state;
836
837 WARN_ON(!lockdep_rtnl_is_held());
838
839 if (pl->phydev) {
840 phy_ethtool_ksettings_get(pl->phydev, kset);
841 } else {
842 kset->base.port = pl->link_port;
843 }
844
845 linkmode_copy(kset->link_modes.supported, pl->supported);
846
847 switch (pl->link_an_mode) {
848 case MLO_AN_FIXED:
849 /* We are using fixed settings. Report these as the
850 * current link settings - and note that these also
851 * represent the supported speeds/duplex/pause modes.
852 */
853 phylink_get_fixed_state(pl, &link_state);
854 phylink_get_ksettings(&link_state, kset);
855 break;
856
Russell King86a362c2017-12-01 10:24:26 +0000857 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100858 /* If there is a phy attached, then use the reported
859 * settings from the phy with no modification.
860 */
861 if (pl->phydev)
862 break;
863
Russell King9525ae82017-07-25 15:03:13 +0100864 phylink_get_mac_state(pl, &link_state);
865
866 /* The MAC is reporting the link results from its own PCS
867 * layer via in-band status. Report these as the current
868 * link settings.
869 */
870 phylink_get_ksettings(&link_state, kset);
871 break;
872 }
873
874 return 0;
875}
876EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
877
878int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700879 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +0100880{
881 struct ethtool_link_ksettings our_kset;
882 struct phylink_link_state config;
883 int ret;
884
885 WARN_ON(!lockdep_rtnl_is_held());
886
887 if (kset->base.autoneg != AUTONEG_DISABLE &&
888 kset->base.autoneg != AUTONEG_ENABLE)
889 return -EINVAL;
890
891 config = pl->link_config;
892
893 /* Mask out unsupported advertisments */
894 linkmode_and(config.advertising, kset->link_modes.advertising,
895 pl->supported);
896
897 /* FIXME: should we reject autoneg if phy/mac does not support it? */
898 if (kset->base.autoneg == AUTONEG_DISABLE) {
899 const struct phy_setting *s;
900
901 /* Autonegotiation disabled, select a suitable speed and
902 * duplex.
903 */
904 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
905 pl->supported,
906 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
907 if (!s)
908 return -EINVAL;
909
910 /* If we have a fixed link (as specified by firmware), refuse
911 * to change link parameters.
912 */
913 if (pl->link_an_mode == MLO_AN_FIXED &&
914 (s->speed != pl->link_config.speed ||
915 s->duplex != pl->link_config.duplex))
916 return -EINVAL;
917
918 config.speed = s->speed;
919 config.duplex = s->duplex;
920 config.an_enabled = false;
921
922 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
923 } else {
924 /* If we have a fixed link, refuse to enable autonegotiation */
925 if (pl->link_an_mode == MLO_AN_FIXED)
926 return -EINVAL;
927
928 config.speed = SPEED_UNKNOWN;
929 config.duplex = DUPLEX_UNKNOWN;
930 config.an_enabled = true;
931
932 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
933 }
934
935 if (phylink_validate(pl, pl->supported, &config))
936 return -EINVAL;
937
938 /* If autonegotiation is enabled, we must have an advertisment */
939 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
940 return -EINVAL;
941
942 our_kset = *kset;
943 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
944 our_kset.base.speed = config.speed;
945 our_kset.base.duplex = config.duplex;
946
947 /* If we have a PHY, configure the phy */
948 if (pl->phydev) {
949 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
950 if (ret)
951 return ret;
952 }
953
954 mutex_lock(&pl->state_mutex);
955 /* Configure the MAC to match the new settings */
956 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
957 pl->link_config.speed = our_kset.base.speed;
958 pl->link_config.duplex = our_kset.base.duplex;
959 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
960
961 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
962 phylink_mac_config(pl, &pl->link_config);
963 phylink_mac_an_restart(pl);
964 }
965 mutex_unlock(&pl->state_mutex);
966
Dan Carpenterd18c2a12017-08-10 00:35:50 +0300967 return 0;
Russell King9525ae82017-07-25 15:03:13 +0100968}
969EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
970
971int phylink_ethtool_nway_reset(struct phylink *pl)
972{
973 int ret = 0;
974
975 WARN_ON(!lockdep_rtnl_is_held());
976
977 if (pl->phydev)
978 ret = phy_restart_aneg(pl->phydev);
979 phylink_mac_an_restart(pl);
980
981 return ret;
982}
983EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
984
985void phylink_ethtool_get_pauseparam(struct phylink *pl,
986 struct ethtool_pauseparam *pause)
987{
988 WARN_ON(!lockdep_rtnl_is_held());
989
990 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
991 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
992 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
993}
994EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
995
996int phylink_ethtool_set_pauseparam(struct phylink *pl,
997 struct ethtool_pauseparam *pause)
998{
999 struct phylink_link_state *config = &pl->link_config;
1000
1001 WARN_ON(!lockdep_rtnl_is_held());
1002
1003 if (!phylink_test(pl->supported, Pause) &&
1004 !phylink_test(pl->supported, Asym_Pause))
1005 return -EOPNOTSUPP;
1006
1007 if (!phylink_test(pl->supported, Asym_Pause) &&
1008 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1009 return -EINVAL;
1010
1011 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1012
1013 if (pause->autoneg)
1014 config->pause |= MLO_PAUSE_AN;
1015 if (pause->rx_pause)
1016 config->pause |= MLO_PAUSE_RX;
1017 if (pause->tx_pause)
1018 config->pause |= MLO_PAUSE_TX;
1019
1020 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1021 switch (pl->link_an_mode) {
1022 case MLO_AN_PHY:
1023 /* Silently mark the carrier down, and then trigger a resolve */
1024 netif_carrier_off(pl->netdev);
1025 phylink_run_resolve(pl);
1026 break;
1027
1028 case MLO_AN_FIXED:
1029 /* Should we allow fixed links to change against the config? */
1030 phylink_resolve_flow(pl, config);
1031 phylink_mac_config(pl, config);
1032 break;
1033
Russell King86a362c2017-12-01 10:24:26 +00001034 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001035 phylink_mac_config(pl, config);
1036 phylink_mac_an_restart(pl);
1037 break;
1038 }
1039 }
1040
1041 return 0;
1042}
1043EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1044
Russell King770a1ad2017-07-25 15:03:23 +01001045int phylink_ethtool_get_module_info(struct phylink *pl,
1046 struct ethtool_modinfo *modinfo)
1047{
1048 int ret = -EOPNOTSUPP;
1049
1050 WARN_ON(!lockdep_rtnl_is_held());
1051
1052 if (pl->sfp_bus)
1053 ret = sfp_get_module_info(pl->sfp_bus, modinfo);
1054
1055 return ret;
1056}
1057EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
1058
1059int phylink_ethtool_get_module_eeprom(struct phylink *pl,
1060 struct ethtool_eeprom *ee, u8 *buf)
1061{
1062 int ret = -EOPNOTSUPP;
1063
1064 WARN_ON(!lockdep_rtnl_is_held());
1065
1066 if (pl->sfp_bus)
1067 ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
1068
1069 return ret;
1070}
1071EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
1072
Russell King9525ae82017-07-25 15:03:13 +01001073int phylink_get_eee_err(struct phylink *pl)
1074{
1075 int ret = 0;
1076
1077 WARN_ON(!lockdep_rtnl_is_held());
1078
1079 if (pl->phydev)
1080 ret = phy_get_eee_err(pl->phydev);
1081
1082 return ret;
1083}
1084EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1085
1086int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1087{
1088 int ret = -EOPNOTSUPP;
1089
1090 WARN_ON(!lockdep_rtnl_is_held());
1091
1092 if (pl->phydev)
1093 ret = phy_ethtool_get_eee(pl->phydev, eee);
1094
1095 return ret;
1096}
1097EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1098
1099int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1100{
1101 int ret = -EOPNOTSUPP;
1102
1103 WARN_ON(!lockdep_rtnl_is_held());
1104
1105 if (pl->phydev)
1106 ret = phy_ethtool_set_eee(pl->phydev, eee);
1107
1108 return ret;
1109}
1110EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1111
1112/* This emulates MII registers for a fixed-mode phy operating as per the
1113 * passed in state. "aneg" defines if we report negotiation is possible.
1114 *
1115 * FIXME: should deal with negotiation state too.
1116 */
1117static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1118 struct phylink_link_state *state, bool aneg)
1119{
1120 struct fixed_phy_status fs;
1121 int val;
1122
1123 fs.link = state->link;
1124 fs.speed = state->speed;
1125 fs.duplex = state->duplex;
1126 fs.pause = state->pause & MLO_PAUSE_SYM;
1127 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1128
1129 val = swphy_read_reg(reg, &fs);
1130 if (reg == MII_BMSR) {
1131 if (!state->an_complete)
1132 val &= ~BMSR_ANEGCOMPLETE;
1133 if (!aneg)
1134 val &= ~BMSR_ANEGCAPABLE;
1135 }
1136 return val;
1137}
1138
Russell Kingecbd87b2017-07-25 15:03:28 +01001139static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1140 unsigned int reg)
1141{
1142 struct phy_device *phydev = pl->phydev;
1143 int prtad, devad;
1144
1145 if (mdio_phy_id_is_c45(phy_id)) {
1146 prtad = mdio_phy_id_prtad(phy_id);
1147 devad = mdio_phy_id_devad(phy_id);
1148 devad = MII_ADDR_C45 | devad << 16 | reg;
1149 } else if (phydev->is_c45) {
1150 switch (reg) {
1151 case MII_BMCR:
1152 case MII_BMSR:
1153 case MII_PHYSID1:
1154 case MII_PHYSID2:
1155 devad = __ffs(phydev->c45_ids.devices_in_package);
1156 break;
1157 case MII_ADVERTISE:
1158 case MII_LPA:
1159 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1160 return -EINVAL;
1161 devad = MDIO_MMD_AN;
1162 if (reg == MII_ADVERTISE)
1163 reg = MDIO_AN_ADVERTISE;
1164 else
1165 reg = MDIO_AN_LPA;
1166 break;
1167 default:
1168 return -EINVAL;
1169 }
1170 prtad = phy_id;
1171 devad = MII_ADDR_C45 | devad << 16 | reg;
1172 } else {
1173 prtad = phy_id;
1174 devad = reg;
1175 }
1176 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1177}
1178
1179static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1180 unsigned int reg, unsigned int val)
1181{
1182 struct phy_device *phydev = pl->phydev;
1183 int prtad, devad;
1184
1185 if (mdio_phy_id_is_c45(phy_id)) {
1186 prtad = mdio_phy_id_prtad(phy_id);
1187 devad = mdio_phy_id_devad(phy_id);
1188 devad = MII_ADDR_C45 | devad << 16 | reg;
1189 } else if (phydev->is_c45) {
1190 switch (reg) {
1191 case MII_BMCR:
1192 case MII_BMSR:
1193 case MII_PHYSID1:
1194 case MII_PHYSID2:
1195 devad = __ffs(phydev->c45_ids.devices_in_package);
1196 break;
1197 case MII_ADVERTISE:
1198 case MII_LPA:
1199 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1200 return -EINVAL;
1201 devad = MDIO_MMD_AN;
1202 if (reg == MII_ADVERTISE)
1203 reg = MDIO_AN_ADVERTISE;
1204 else
1205 reg = MDIO_AN_LPA;
1206 break;
1207 default:
1208 return -EINVAL;
1209 }
1210 prtad = phy_id;
1211 devad = MII_ADDR_C45 | devad << 16 | reg;
1212 } else {
1213 prtad = phy_id;
1214 devad = reg;
1215 }
1216
1217 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1218}
1219
Russell King9525ae82017-07-25 15:03:13 +01001220static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1221 unsigned int reg)
1222{
1223 struct phylink_link_state state;
1224 int val = 0xffff;
1225
Russell King9525ae82017-07-25 15:03:13 +01001226 switch (pl->link_an_mode) {
1227 case MLO_AN_FIXED:
1228 if (phy_id == 0) {
1229 phylink_get_fixed_state(pl, &state);
1230 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1231 true);
1232 }
1233 break;
1234
1235 case MLO_AN_PHY:
1236 return -EOPNOTSUPP;
1237
Russell King86a362c2017-12-01 10:24:26 +00001238 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001239 if (phy_id == 0) {
1240 val = phylink_get_mac_state(pl, &state);
1241 if (val < 0)
1242 return val;
1243
1244 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1245 true);
1246 }
1247 break;
1248 }
1249
1250 return val & 0xffff;
1251}
1252
1253static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1254 unsigned int reg, unsigned int val)
1255{
Russell King9525ae82017-07-25 15:03:13 +01001256 switch (pl->link_an_mode) {
1257 case MLO_AN_FIXED:
1258 break;
1259
1260 case MLO_AN_PHY:
1261 return -EOPNOTSUPP;
1262
Russell King86a362c2017-12-01 10:24:26 +00001263 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001264 break;
1265 }
1266
1267 return 0;
1268}
1269
1270int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1271{
Russell Kingecbd87b2017-07-25 15:03:28 +01001272 struct mii_ioctl_data *mii = if_mii(ifr);
1273 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001274
1275 WARN_ON(!lockdep_rtnl_is_held());
1276
Russell Kingecbd87b2017-07-25 15:03:28 +01001277 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001278 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001279 switch (cmd) {
1280 case SIOCGMIIPHY:
1281 mii->phy_id = pl->phydev->mdio.addr;
Russell King9525ae82017-07-25 15:03:13 +01001282
Russell Kingecbd87b2017-07-25 15:03:28 +01001283 case SIOCGMIIREG:
1284 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1285 if (ret >= 0) {
1286 mii->val_out = ret;
1287 ret = 0;
1288 }
1289 break;
Russell King9525ae82017-07-25 15:03:13 +01001290
Russell Kingecbd87b2017-07-25 15:03:28 +01001291 case SIOCSMIIREG:
1292 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1293 mii->val_in);
1294 break;
Russell King9525ae82017-07-25 15:03:13 +01001295
Russell Kingecbd87b2017-07-25 15:03:28 +01001296 default:
Russell King9525ae82017-07-25 15:03:13 +01001297 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001298 break;
1299 }
1300 } else {
1301 switch (cmd) {
1302 case SIOCGMIIPHY:
1303 mii->phy_id = 0;
1304
1305 case SIOCGMIIREG:
1306 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1307 if (ret >= 0) {
1308 mii->val_out = ret;
1309 ret = 0;
1310 }
1311 break;
1312
1313 case SIOCSMIIREG:
1314 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1315 mii->val_in);
1316 break;
1317
1318 default:
1319 ret = -EOPNOTSUPP;
1320 break;
1321 }
Russell King9525ae82017-07-25 15:03:13 +01001322 }
1323
1324 return ret;
1325}
1326EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1327
Russell Kingce0aa272017-07-25 15:03:18 +01001328static int phylink_sfp_module_insert(void *upstream,
1329 const struct sfp_eeprom_id *id)
1330{
1331 struct phylink *pl = upstream;
1332 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1333 struct phylink_link_state config;
1334 phy_interface_t iface;
1335 int mode, ret = 0;
1336 bool changed;
1337 u8 port;
1338
1339 sfp_parse_support(pl->sfp_bus, id, support);
1340 port = sfp_parse_port(pl->sfp_bus, id, support);
1341 iface = sfp_parse_interface(pl->sfp_bus, id);
1342
1343 WARN_ON(!lockdep_rtnl_is_held());
1344
1345 switch (iface) {
1346 case PHY_INTERFACE_MODE_SGMII:
Russell Kingce0aa272017-07-25 15:03:18 +01001347 case PHY_INTERFACE_MODE_1000BASEX:
Russell King4336c402017-12-01 10:24:32 +00001348 case PHY_INTERFACE_MODE_2500BASEX:
1349 case PHY_INTERFACE_MODE_10GKR:
Russell King86a362c2017-12-01 10:24:26 +00001350 mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001351 break;
1352 default:
1353 return -EINVAL;
1354 }
1355
1356 memset(&config, 0, sizeof(config));
1357 linkmode_copy(config.advertising, support);
1358 config.interface = iface;
1359 config.speed = SPEED_UNKNOWN;
1360 config.duplex = DUPLEX_UNKNOWN;
1361 config.pause = MLO_PAUSE_AN;
1362 config.an_enabled = pl->link_config.an_enabled;
1363
1364 /* Ignore errors if we're expecting a PHY to attach later */
1365 ret = phylink_validate(pl, support, &config);
1366 if (ret) {
1367 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1368 phylink_an_mode_str(mode), phy_modes(config.interface),
1369 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1370 return ret;
1371 }
1372
1373 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1374 phylink_an_mode_str(mode), phy_modes(config.interface),
1375 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1376
Russell King86a362c2017-12-01 10:24:26 +00001377 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001378 return -EINVAL;
1379
1380 changed = !bitmap_equal(pl->supported, support,
1381 __ETHTOOL_LINK_MODE_MASK_NBITS);
1382 if (changed) {
1383 linkmode_copy(pl->supported, support);
1384 linkmode_copy(pl->link_config.advertising, config.advertising);
1385 }
1386
1387 if (pl->link_an_mode != mode ||
1388 pl->link_config.interface != config.interface) {
1389 pl->link_config.interface = config.interface;
1390 pl->link_an_mode = mode;
1391
1392 changed = true;
1393
1394 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1395 phylink_an_mode_str(mode),
1396 phy_modes(config.interface));
1397 }
1398
1399 pl->link_port = port;
1400
1401 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1402 &pl->phylink_disable_state))
1403 phylink_mac_config(pl, &pl->link_config);
1404
1405 return ret;
1406}
1407
1408static void phylink_sfp_link_down(void *upstream)
1409{
1410 struct phylink *pl = upstream;
1411
1412 WARN_ON(!lockdep_rtnl_is_held());
1413
1414 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1415 flush_work(&pl->resolve);
1416
1417 netif_carrier_off(pl->netdev);
1418}
1419
1420static void phylink_sfp_link_up(void *upstream)
1421{
1422 struct phylink *pl = upstream;
1423
1424 WARN_ON(!lockdep_rtnl_is_held());
1425
1426 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1427 phylink_run_resolve(pl);
1428}
1429
1430static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1431{
1432 return phylink_connect_phy(upstream, phy);
1433}
1434
1435static void phylink_sfp_disconnect_phy(void *upstream)
1436{
1437 phylink_disconnect_phy(upstream);
1438}
1439
1440static const struct sfp_upstream_ops sfp_phylink_ops = {
1441 .module_insert = phylink_sfp_module_insert,
1442 .link_up = phylink_sfp_link_up,
1443 .link_down = phylink_sfp_link_down,
1444 .connect_phy = phylink_sfp_connect_phy,
1445 .disconnect_phy = phylink_sfp_disconnect_phy,
1446};
1447
Russell King9525ae82017-07-25 15:03:13 +01001448MODULE_LICENSE("GPL");