kernel: bump 4.9 to 4.9.63
[openwrt/openwrt.git] / target / linux / mvebu / patches-4.9 / 415-phylink-add-phylink-infrastructure.patch
1 From: Russell King <rmk+kernel@arm.linux.org.uk>
2 Date: Tue, 22 Sep 2015 20:52:18 +0100
3 Subject: [PATCH] phylink: add phylink infrastructure
4
5 The link between the ethernet MAC and its PHY has become more complex
6 as the interface evolves. This is especially true with serdes links,
7 where the part of the PHY is effectively integrated into the MAC.
8
9 Serdes links can be connected to a variety of devices, including SFF
10 modules soldered down onto the board with the MAC, a SFP cage with
11 a hotpluggable SFP module which may contain a PHY or directly modulate
12 the serdes signals onto optical media with or without a PHY, or even
13 a classical PHY connection.
14
15 Moreover, the negotiation information on serdes links comes in two
16 varieties - SGMII mode, where the PHY provides its speed/duplex/flow
17 control information to the MAC, and 1000base-X mode where both ends
18 exchange their abilities and each resolve the link capabilities.
19
20 This means we need a more flexible means to support these arrangements,
21 particularly with the hotpluggable nature of SFP, where the PHY can
22 be attached or detached after the network device has been brought up.
23
24 Ethtool information can come from multiple sources:
25 - we may have a PHY operating in either SGMII or 1000base-X mode, in
26 which case we take ethtool/mii data directly from the PHY.
27 - we may have a optical SFP module without a PHY, with the MAC
28 operating in 1000base-X mode - the ethtool/mii data needs to come
29 from the MAC.
30 - we may have a copper SFP module with a PHY whic can't be accessed,
31 which means we need to take ethtool/mii data from the MAC.
32
33 Phylink aims to solve this by providing an intermediary between the
34 MAC and PHY, providing a safe way for PHYs to be hotplugged, and
35 allowing a SFP driver to reconfigure the serdes connection.
36
37 Phylink also takes over support of fixed link connections, where the
38 speed/duplex/flow control are fixed, but link status may be controlled
39 by a GPIO signal. By avoiding the fixed-phy implementation, phylink
40 can provide a faster response to link events: fixed-phy has to wait for
41 phylib to operate its state machine, which can take several seconds.
42 In comparison, phylink takes milliseconds.
43
44 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
45
46 - remove sync status
47 - rework supported and advertisment handling
48 - add 1000base-x speed for fixed links
49 - use functionality exported from phy-core, reworking
50 __phylink_ethtool_ksettings_set for it
51 ---
52 create mode 100644 drivers/net/phy/phylink.c
53 create mode 100644 include/linux/phylink.h
54
55 --- a/drivers/net/phy/Kconfig
56 +++ b/drivers/net/phy/Kconfig
57 @@ -10,6 +10,16 @@ menuconfig PHYLIB
58 devices. This option provides infrastructure for
59 managing PHY devices.
60
61 +config PHYLINK
62 + tristate
63 + depends on NETDEVICES
64 + select PHYLIB
65 + select SWPHY
66 + help
67 + PHYlink models the link between the PHY and MAC, allowing fixed
68 + configuration links, PHYs, and Serdes links with MAC level
69 + autonegotiation modes.
70 +
71 if PHYLIB
72
73 config SWPHY
74 --- a/drivers/net/phy/Makefile
75 +++ b/drivers/net/phy/Makefile
76 @@ -6,6 +6,7 @@ libphy-$(CONFIG_SWPHY) += swphy.o
77
78 obj-$(CONFIG_MDIO_BOARDINFO) += mdio-boardinfo.o
79
80 +obj-$(CONFIG_PHYLINK) += phylink.o
81 obj-$(CONFIG_PHYLIB) += libphy.o
82
83 obj-$(CONFIG_SWCONFIG) += swconfig.o
84 --- a/drivers/net/phy/phy_device.c
85 +++ b/drivers/net/phy/phy_device.c
86 @@ -1021,6 +1021,7 @@ void phy_detach(struct phy_device *phyde
87 phydev->attached_dev->phydev = NULL;
88 phydev->attached_dev = NULL;
89 phy_suspend(phydev);
90 + phydev->phylink = NULL;
91
92 module_put(phydev->mdio.dev.driver->owner);
93
94 --- /dev/null
95 +++ b/drivers/net/phy/phylink.c
96 @@ -0,0 +1,903 @@
97 +/*
98 + * phylink models the MAC to optional PHY connection, supporting
99 + * technologies such as SFP cages where the PHY is hot-pluggable.
100 + *
101 + * Copyright (C) 2015 Russell King
102 + *
103 + * This program is free software; you can redistribute it and/or modify
104 + * it under the terms of the GNU General Public License version 2 as
105 + * published by the Free Software Foundation.
106 + */
107 +#include <linux/ethtool.h>
108 +#include <linux/export.h>
109 +#include <linux/gpio/consumer.h>
110 +#include <linux/netdevice.h>
111 +#include <linux/of.h>
112 +#include <linux/of_mdio.h>
113 +#include <linux/phy.h>
114 +#include <linux/phy_fixed.h>
115 +#include <linux/phylink.h>
116 +#include <linux/spinlock.h>
117 +#include <linux/workqueue.h>
118 +
119 +#include "swphy.h"
120 +
121 +#define SUPPORTED_INTERFACES \
122 + (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
123 + SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
124 +#define ADVERTISED_INTERFACES \
125 + (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
126 + ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
127 +
128 +enum {
129 + PHYLINK_DISABLE_STOPPED,
130 +};
131 +
132 +struct phylink {
133 + struct net_device *netdev;
134 + const struct phylink_mac_ops *ops;
135 + struct mutex config_mutex;
136 +
137 + unsigned long phylink_disable_state; /* bitmask of disables */
138 + struct phy_device *phydev;
139 + phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
140 + u8 link_an_mode; /* MLO_AN_xxx */
141 + u8 link_port; /* The current non-phy ethtool port */
142 + /* ethtool supported mask for ports */
143 + __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
144 +
145 + /* The link configuration settings */
146 + struct phylink_link_state link_config;
147 + struct gpio_desc *link_gpio;
148 +
149 + struct mutex state_mutex; /* may be taken within config_mutex */
150 + struct phylink_link_state phy_state;
151 + struct work_struct resolve;
152 +
153 + const struct phylink_module_ops *module_ops;
154 + void *module_data;
155 +};
156 +
157 +static inline void linkmode_zero(unsigned long *dst)
158 +{
159 + bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
160 +}
161 +
162 +static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
163 +{
164 + bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
165 +}
166 +
167 +static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
168 + const unsigned long *b)
169 +{
170 + bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
171 +}
172 +
173 +static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
174 + const unsigned long *b)
175 +{
176 + bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
177 +}
178 +
179 +static inline bool linkmode_empty(const unsigned long *src)
180 +{
181 + return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
182 +}
183 +
184 +static void phylink_set_port_bits(unsigned long *bits)
185 +{
186 + __set_bit(ETHTOOL_LINK_MODE_TP_BIT, bits);
187 + __set_bit(ETHTOOL_LINK_MODE_AUI_BIT, bits);
188 + __set_bit(ETHTOOL_LINK_MODE_MII_BIT, bits);
189 + __set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, bits);
190 + __set_bit(ETHTOOL_LINK_MODE_BNC_BIT, bits);
191 + __set_bit(ETHTOOL_LINK_MODE_Backplane_BIT, bits);
192 +}
193 +
194 +static const char *phylink_an_mode_str(unsigned int mode)
195 +{
196 + static const char *modestr[] = {
197 + [MLO_AN_PHY] = "phy",
198 + [MLO_AN_FIXED] = "fixed",
199 + [MLO_AN_SGMII] = "SGMII",
200 + [MLO_AN_8023Z] = "802.3z",
201 + };
202 +
203 + return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
204 +}
205 +
206 +static int phylink_parse_fixedlink(struct phylink *pl, struct device_node *np)
207 +{
208 + __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
209 + struct device_node *fixed_node;
210 + const struct phy_setting *s;
211 + struct gpio_desc *desc;
212 + const __be32 *fixed_prop;
213 + u32 speed;
214 + int ret, len;
215 +
216 + fixed_node = of_get_child_by_name(np, "fixed-link");
217 + if (fixed_node) {
218 + ret = of_property_read_u32(fixed_node, "speed", &speed);
219 +
220 + pl->link_config.speed = speed;
221 + pl->link_config.duplex = DUPLEX_HALF;
222 +
223 + if (of_property_read_bool(fixed_node, "full-duplex"))
224 + pl->link_config.duplex = DUPLEX_FULL;
225 + if (of_property_read_bool(fixed_node, "pause"))
226 + pl->link_config.pause |= MLO_PAUSE_SYM;
227 + if (of_property_read_bool(fixed_node, "asym-pause"))
228 + pl->link_config.pause |= MLO_PAUSE_ASYM;
229 +
230 + if (ret == 0) {
231 + desc = fwnode_get_named_gpiod(&fixed_node->fwnode,
232 + "link-gpios");
233 +
234 + if (!IS_ERR(desc))
235 + pl->link_gpio = desc;
236 + else if (desc == ERR_PTR(-EPROBE_DEFER))
237 + ret = -EPROBE_DEFER;
238 + }
239 + of_node_put(fixed_node);
240 +
241 + if (ret)
242 + return ret;
243 + } else {
244 + fixed_prop = of_get_property(np, "fixed-link", &len);
245 + if (!fixed_prop) {
246 + netdev_err(pl->netdev, "broken fixed-link?\n");
247 + return -EINVAL;
248 + }
249 + if (len == 5 * sizeof(*fixed_prop)) {
250 + pl->link_config.duplex = be32_to_cpu(fixed_prop[1]) ?
251 + DUPLEX_FULL : DUPLEX_HALF;
252 + pl->link_config.speed = be32_to_cpu(fixed_prop[2]);
253 + if (be32_to_cpu(fixed_prop[3]))
254 + pl->link_config.pause |= MLO_PAUSE_SYM;
255 + if (be32_to_cpu(fixed_prop[4]))
256 + pl->link_config.pause |= MLO_PAUSE_ASYM;
257 + }
258 + }
259 +
260 + bitmap_fill(mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
261 + pl->ops->validate_support(pl->netdev, MLO_AN_FIXED, mask);
262 +
263 + pl->link_config.link = 1;
264 + pl->link_config.an_complete = 1;
265 +
266 + if (pl->link_config.speed > SPEED_1000 &&
267 + pl->link_config.duplex != DUPLEX_FULL)
268 + netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
269 + pl->link_config.speed);
270 +
271 + s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
272 + mask, __ETHTOOL_LINK_MODE_MASK_NBITS, true);
273 + if (s) {
274 + __set_bit(s->bit, pl->supported);
275 + } else {
276 + netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
277 + pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
278 + pl->link_config.speed);
279 + }
280 + return 0;
281 +}
282 +
283 +static int phylink_parse_mode(struct phylink *pl, struct device_node *np)
284 +{
285 + struct device_node *dn;
286 + const char *managed;
287 +
288 + dn = of_get_child_by_name(np, "fixed-link");
289 + if (dn || of_find_property(np, "fixed-link", NULL))
290 + pl->link_an_mode = MLO_AN_FIXED;
291 + of_node_put(dn);
292 +
293 + if (of_property_read_string(np, "managed", &managed) == 0 &&
294 + strcmp(managed, "in-band-status") == 0) {
295 + if (pl->link_an_mode == MLO_AN_FIXED) {
296 + netdev_err(pl->netdev,
297 + "can't use both fixed-link and in-band-status\n");
298 + return -EINVAL;
299 + }
300 + phylink_set(pl->supported, 10baseT_Half);
301 + phylink_set(pl->supported, 10baseT_Full);
302 + phylink_set(pl->supported, 100baseT_Half);
303 + phylink_set(pl->supported, 100baseT_Full);
304 + phylink_set(pl->supported, 1000baseT_Half);
305 + phylink_set(pl->supported, 1000baseT_Full);
306 + phylink_set(pl->supported, Asym_Pause);
307 + phylink_set(pl->supported, Pause);
308 + pl->link_an_mode = MLO_AN_SGMII;
309 + pl->link_config.an_enabled = true;
310 + pl->ops->validate_support(pl->netdev, pl->link_an_mode,
311 + pl->supported);
312 + }
313 +
314 + return 0;
315 +}
316 +
317 +
318 +static void phylink_init_advert(struct phylink *pl, unsigned int mode,
319 + const unsigned long *supported,
320 + unsigned long *advertising)
321 +{
322 + linkmode_copy(advertising, supported);
323 + if (pl->ops->validate_advert)
324 + pl->ops->validate_advert(pl->netdev, mode, supported,
325 + advertising);
326 +}
327 +
328 +static void phylink_mac_config(struct phylink *pl,
329 + const struct phylink_link_state *state)
330 +{
331 + netdev_dbg(pl->netdev,
332 + "%s: mode=%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
333 + __func__, phylink_an_mode_str(pl->link_an_mode),
334 + phy_speed_to_str(state->speed),
335 + phy_duplex_to_str(state->duplex),
336 + __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
337 + state->pause, state->link, state->an_enabled);
338 +
339 + pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
340 +}
341 +
342 +static void phylink_mac_an_restart(struct phylink *pl)
343 +{
344 + if (pl->link_config.an_enabled)
345 + pl->ops->mac_an_restart(pl->netdev, pl->link_an_mode);
346 +}
347 +
348 +static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
349 +{
350 + struct net_device *ndev = pl->netdev;
351 +
352 + linkmode_copy(state->advertising, pl->link_config.advertising);
353 + linkmode_zero(state->lp_advertising);
354 + state->an_enabled = pl->link_config.an_enabled;
355 + state->link = 1;
356 +
357 + return pl->ops->mac_link_state(ndev, state);
358 +}
359 +
360 +/* The fixed state is... fixed except for the link state,
361 + * which may be determined by a GPIO.
362 + */
363 +static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
364 +{
365 + *state = pl->link_config;
366 + if (pl->link_gpio)
367 + state->link = !!gpiod_get_value(pl->link_gpio);
368 +}
369 +
370 +static void phylink_resolve(struct work_struct *w)
371 +{
372 + struct phylink *pl = container_of(w, struct phylink, resolve);
373 + struct phylink_link_state link_state;
374 + struct net_device *ndev = pl->netdev;
375 +
376 + mutex_lock(&pl->state_mutex);
377 + if (pl->phylink_disable_state) {
378 + link_state.link = false;
379 + } else {
380 + switch (pl->link_an_mode) {
381 + case MLO_AN_PHY:
382 + link_state = pl->phy_state;
383 + break;
384 +
385 + case MLO_AN_FIXED:
386 + phylink_get_fixed_state(pl, &link_state);
387 + break;
388 +
389 + case MLO_AN_SGMII:
390 + phylink_get_mac_state(pl, &link_state);
391 + if (pl->phydev)
392 + link_state.link = link_state.link &&
393 + pl->phy_state.link;
394 + break;
395 +
396 + case MLO_AN_8023Z:
397 + phylink_get_mac_state(pl, &link_state);
398 + break;
399 + }
400 + }
401 +
402 + if (link_state.link != netif_carrier_ok(ndev)) {
403 + if (!link_state.link) {
404 + netif_carrier_off(ndev);
405 + pl->ops->mac_link_down(ndev, pl->link_an_mode);
406 + netdev_info(ndev, "Link is Down\n");
407 + } else {
408 + /* If we have a PHY, we need the MAC updated with
409 + * the current link parameters (eg, in SGMII mode,
410 + * with flow control status.)
411 + */
412 + if (pl->phydev)
413 + phylink_mac_config(pl, &link_state);
414 +
415 + pl->ops->mac_link_up(ndev, pl->link_an_mode);
416 +
417 + netif_carrier_on(ndev);
418 +
419 + netdev_info(ndev,
420 + "Link is Up - %s/%s - flow control %s\n",
421 + phy_speed_to_str(link_state.speed),
422 + phy_duplex_to_str(link_state.duplex),
423 + link_state.pause ? "rx/tx" : "off");
424 + }
425 + }
426 + mutex_unlock(&pl->state_mutex);
427 +}
428 +
429 +static void phylink_run_resolve(struct phylink *pl)
430 +{
431 + if (!pl->phylink_disable_state)
432 + queue_work(system_power_efficient_wq, &pl->resolve);
433 +}
434 +
435 +struct phylink *phylink_create(struct net_device *ndev, struct device_node *np,
436 + phy_interface_t iface, const struct phylink_mac_ops *ops)
437 +{
438 + struct phylink *pl;
439 + int ret;
440 +
441 + pl = kzalloc(sizeof(*pl), GFP_KERNEL);
442 + if (!pl)
443 + return ERR_PTR(-ENOMEM);
444 +
445 + mutex_init(&pl->state_mutex);
446 + mutex_init(&pl->config_mutex);
447 + INIT_WORK(&pl->resolve, phylink_resolve);
448 + pl->netdev = ndev;
449 + pl->link_interface = iface;
450 + pl->link_port = PORT_MII;
451 + pl->link_config.speed = SPEED_UNKNOWN;
452 + pl->link_config.duplex = DUPLEX_UNKNOWN;
453 + pl->ops = ops;
454 + __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
455 +
456 + ret = phylink_parse_mode(pl, np);
457 + if (ret < 0) {
458 + kfree(pl);
459 + return ERR_PTR(ret);
460 + }
461 +
462 + if (pl->link_an_mode == MLO_AN_FIXED) {
463 + ret = phylink_parse_fixedlink(pl, np);
464 + if (ret < 0) {
465 + kfree(pl);
466 + return ERR_PTR(ret);
467 + }
468 + }
469 +
470 + phylink_set(pl->supported, MII);
471 + phylink_init_advert(pl, pl->link_an_mode, pl->supported,
472 + pl->link_config.advertising);
473 +
474 + return pl;
475 +}
476 +EXPORT_SYMBOL_GPL(phylink_create);
477 +
478 +void phylink_destroy(struct phylink *pl)
479 +{
480 + cancel_work_sync(&pl->resolve);
481 + kfree(pl);
482 +}
483 +EXPORT_SYMBOL_GPL(phylink_destroy);
484 +
485 +void phylink_phy_change(struct phy_device *phydev, bool up, bool do_carrier)
486 +{
487 + struct phylink *pl = phydev->phylink;
488 +
489 + mutex_lock(&pl->state_mutex);
490 + pl->phy_state.speed = phydev->speed;
491 + pl->phy_state.duplex = phydev->duplex;
492 + pl->phy_state.pause = MLO_PAUSE_NONE;
493 + if (phydev->pause)
494 + pl->phy_state.pause |= MLO_PAUSE_SYM;
495 + if (phydev->asym_pause)
496 + pl->phy_state.pause |= MLO_PAUSE_ASYM;
497 + pl->phy_state.link = up;
498 + mutex_unlock(&pl->state_mutex);
499 +
500 + phylink_run_resolve(pl);
501 +
502 + netdev_dbg(pl->netdev, "phy link %s %s/%s\n", up ? "up" : "down",
503 + phy_speed_to_str(phydev->speed),
504 + phy_duplex_to_str(phydev->duplex));
505 +}
506 +
507 +static int phylink_empty_linkmode(const unsigned long *linkmode)
508 +{
509 + __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
510 +
511 + phylink_set_port_bits(tmp);
512 + phylink_set(tmp, Autoneg);
513 + phylink_set(tmp, Pause);
514 + phylink_set(tmp, Asym_Pause);
515 +
516 + bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
517 +
518 + return linkmode_empty(tmp);
519 +}
520 +
521 +static int phylink_validate_support(struct phylink *pl, int mode,
522 + unsigned long *mask)
523 +{
524 + pl->ops->validate_support(pl->netdev, mode, mask);
525 +
526 + return phylink_empty_linkmode(mask) ? -EINVAL : 0;
527 +}
528 +
529 +static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
530 +{
531 + __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
532 + u32 advertising;
533 + int ret;
534 +
535 + ethtool_convert_legacy_u32_to_link_mode(mask, phy->supported);
536 + ret = phylink_validate_support(pl, pl->link_an_mode, mask);
537 + if (ret)
538 + return ret;
539 +
540 + mutex_lock(&pl->config_mutex);
541 + phy->phylink = pl;
542 + phy->phy_link_change = phylink_phy_change;
543 +
544 + netdev_info(pl->netdev,
545 + "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
546 + phy->drv->name);
547 +
548 + mutex_lock(&pl->state_mutex);
549 + pl->phydev = phy;
550 + linkmode_copy(pl->supported, mask);
551 +
552 + /* Restrict the phy advertisment according to the MAC support. */
553 + ethtool_convert_link_mode_to_legacy_u32(&advertising, mask);
554 + phy->advertising &= ADVERTISED_INTERFACES | advertising;
555 + ethtool_convert_legacy_u32_to_link_mode(pl->link_config.advertising,
556 + phy->advertising);
557 + mutex_unlock(&pl->state_mutex);
558 +
559 + netdev_dbg(pl->netdev,
560 + "phy: setting supported %*pb advertising 0x%08x\n",
561 + __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
562 + phy->advertising);
563 +
564 + phy_start_machine(phy);
565 + if (phy->irq > 0)
566 + phy_start_interrupts(phy);
567 +
568 + mutex_unlock(&pl->config_mutex);
569 +
570 + return 0;
571 +}
572 +
573 +int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
574 +{
575 + int ret;
576 +
577 + ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
578 + if (ret)
579 + return ret;
580 +
581 + ret = phylink_bringup_phy(pl, phy);
582 + if (ret)
583 + phy_detach(phy);
584 +
585 + return ret;
586 +}
587 +EXPORT_SYMBOL_GPL(phylink_connect_phy);
588 +
589 +int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn)
590 +{
591 + struct device_node *phy_node;
592 + struct phy_device *phy_dev;
593 + int ret;
594 +
595 + /* Fixed links are handled without needing a PHY */
596 + if (pl->link_an_mode == MLO_AN_FIXED)
597 + return 0;
598 +
599 + phy_node = of_parse_phandle(dn, "phy-handle", 0);
600 + if (!phy_node)
601 + phy_node = of_parse_phandle(dn, "phy", 0);
602 + if (!phy_node)
603 + phy_node = of_parse_phandle(dn, "phy-device", 0);
604 +
605 + if (!phy_node) {
606 + if (pl->link_an_mode == MLO_AN_PHY) {
607 + netdev_err(pl->netdev, "unable to find PHY node\n");
608 + return -ENODEV;
609 + }
610 + return 0;
611 + }
612 +
613 + phy_dev = of_phy_attach(pl->netdev, phy_node, 0, pl->link_interface);
614 + /* We're done with the phy_node handle */
615 + of_node_put(phy_node);
616 +
617 + if (!phy_dev)
618 + return -ENODEV;
619 +
620 + ret = phylink_bringup_phy(pl, phy_dev);
621 + if (ret)
622 + phy_detach(phy_dev);
623 +
624 + return ret;
625 +}
626 +EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
627 +
628 +void phylink_disconnect_phy(struct phylink *pl)
629 +{
630 + struct phy_device *phy;
631 +
632 + mutex_lock(&pl->config_mutex);
633 + phy = pl->phydev;
634 +
635 + mutex_lock(&pl->state_mutex);
636 + pl->phydev = NULL;
637 + mutex_unlock(&pl->state_mutex);
638 + flush_work(&pl->resolve);
639 +
640 + if (phy)
641 + phy_disconnect(phy);
642 +
643 + mutex_unlock(&pl->config_mutex);
644 +}
645 +EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
646 +
647 +void phylink_mac_change(struct phylink *pl, bool up)
648 +{
649 + phylink_run_resolve(pl);
650 + netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
651 +}
652 +EXPORT_SYMBOL_GPL(phylink_mac_change);
653 +
654 +void phylink_start(struct phylink *pl)
655 +{
656 + mutex_lock(&pl->config_mutex);
657 +
658 + netdev_info(pl->netdev, "configuring for %s link mode\n",
659 + phylink_an_mode_str(pl->link_an_mode));
660 +
661 + /* Apply the link configuration to the MAC when starting. This allows
662 + * a fixed-link to start with the correct parameters, and also
663 + * ensures that we set the appropriate advertisment for Serdes links.
664 + */
665 + phylink_mac_config(pl, &pl->link_config);
666 +
667 + clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
668 + phylink_run_resolve(pl);
669 +
670 + if (pl->phydev)
671 + phy_start(pl->phydev);
672 +
673 + mutex_unlock(&pl->config_mutex);
674 +}
675 +EXPORT_SYMBOL_GPL(phylink_start);
676 +
677 +void phylink_stop(struct phylink *pl)
678 +{
679 + mutex_lock(&pl->config_mutex);
680 +
681 + if (pl->phydev)
682 + phy_stop(pl->phydev);
683 +
684 + set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
685 + flush_work(&pl->resolve);
686 +
687 + mutex_unlock(&pl->config_mutex);
688 +}
689 +EXPORT_SYMBOL_GPL(phylink_stop);
690 +
691 +static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
692 +{
693 + __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
694 +
695 + linkmode_zero(mask);
696 + phylink_set_port_bits(mask);
697 +
698 + linkmode_and(dst, dst, mask);
699 + linkmode_or(dst, dst, b);
700 +}
701 +
702 +static void phylink_get_ksettings(const struct phylink_link_state *state,
703 + struct ethtool_link_ksettings *kset)
704 +{
705 + phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
706 + linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
707 + kset->base.speed = state->speed;
708 + kset->base.duplex = state->duplex;
709 + kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
710 + AUTONEG_DISABLE;
711 +}
712 +
713 +static int __phylink_ethtool_ksettings_get(struct phylink *pl,
714 + struct ethtool_link_ksettings *kset)
715 +{
716 + struct phylink_link_state link_state;
717 + int ret;
718 +
719 + if (pl->phydev) {
720 + ret = phy_ethtool_ksettings_get(pl->phydev, kset);
721 + if (ret)
722 + return ret;
723 + } else {
724 + kset->base.port = pl->link_port;
725 + }
726 +
727 + linkmode_copy(kset->link_modes.supported, pl->supported);
728 +
729 + switch (pl->link_an_mode) {
730 + case MLO_AN_FIXED:
731 + /* We are using fixed settings. Report these as the
732 + * current link settings - and note that these also
733 + * represent the supported speeds/duplex/pause modes.
734 + */
735 + phylink_get_fixed_state(pl, &link_state);
736 + phylink_get_ksettings(&link_state, kset);
737 + break;
738 +
739 + case MLO_AN_SGMII:
740 + /* If there is a phy attached, then use the reported
741 + * settings from the phy with no modification.
742 + */
743 + if (pl->phydev)
744 + break;
745 +
746 + case MLO_AN_8023Z:
747 + phylink_get_mac_state(pl, &link_state);
748 +
749 + /* The MAC is reporting the link results from its own PCS
750 + * layer via in-band status. Report these as the current
751 + * link settings.
752 + */
753 + phylink_get_ksettings(&link_state, kset);
754 + break;
755 + }
756 +
757 + return 0;
758 +}
759 +
760 +int phylink_ethtool_ksettings_get(struct phylink *pl,
761 + struct ethtool_link_ksettings *kset)
762 +{
763 + int ret;
764 +
765 + mutex_lock(&pl->config_mutex);
766 + ret = __phylink_ethtool_ksettings_get(pl, kset);
767 + mutex_unlock(&pl->config_mutex);
768 +
769 + return ret;
770 +}
771 +EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
772 +
773 +static int __phylink_ethtool_ksettings_set(struct phylink *pl,
774 + const struct ethtool_link_ksettings *kset)
775 +{
776 + struct ethtool_link_ksettings our_kset = *kset;
777 + int ret;
778 +
779 + /* Mask out unsupported advertisments */
780 + linkmode_and(our_kset.link_modes.advertising,
781 + kset->link_modes.advertising, pl->supported);
782 +
783 + if (pl->ops->validate_advert)
784 + pl->ops->validate_advert(pl->netdev, pl->link_an_mode,
785 + pl->supported,
786 + our_kset.link_modes.advertising);
787 +
788 + /* FIXME: should we reject autoneg if phy/mac does not support it? */
789 +
790 + if (kset->base.autoneg == AUTONEG_DISABLE) {
791 + const struct phy_setting *s;
792 +
793 + /* Autonegotiation disabled, select a suitable speed and
794 + * duplex.
795 + */
796 + s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
797 + pl->supported,
798 + __ETHTOOL_LINK_MODE_MASK_NBITS, false);
799 + if (!s)
800 + return -EINVAL;
801 +
802 + /* If we have a fixed link (as specified by firmware), refuse
803 + * to change link parameters.
804 + */
805 + if (pl->link_an_mode == MLO_AN_FIXED &&
806 + (s->speed != pl->link_config.speed ||
807 + s->duplex != pl->link_config.duplex))
808 + return -EINVAL;
809 +
810 + our_kset.base.speed = s->speed;
811 + our_kset.base.duplex = s->duplex;
812 +
813 + __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
814 + our_kset.link_modes.advertising);
815 + } else {
816 + /* If we have a fixed link, refuse to enable autonegotiation */
817 + if (pl->link_an_mode == MLO_AN_FIXED)
818 + return -EINVAL;
819 +
820 + /* Autonegotiation enabled, validate advertisment */
821 + if (phylink_empty_linkmode(our_kset.link_modes.advertising))
822 + return -EINVAL;
823 +
824 + __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
825 + our_kset.link_modes.advertising);
826 + }
827 +
828 + /* If we have a PHY, configure the phy */
829 + if (pl->phydev) {
830 + ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
831 + if (ret)
832 + return ret;
833 + }
834 +
835 + mutex_lock(&pl->state_mutex);
836 + /* Configure the MAC to match the new settings */
837 + linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
838 + pl->link_config.speed = our_kset.base.speed;
839 + pl->link_config.duplex = our_kset.base.duplex;
840 + pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
841 +
842 + if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
843 + phylink_mac_config(pl, &pl->link_config);
844 + phylink_mac_an_restart(pl);
845 + }
846 + mutex_unlock(&pl->state_mutex);
847 +
848 + return ret;
849 +}
850 +
851 +int phylink_ethtool_ksettings_set(struct phylink *pl,
852 + const struct ethtool_link_ksettings *kset)
853 +{
854 + int ret;
855 +
856 + if (kset->base.autoneg != AUTONEG_DISABLE &&
857 + kset->base.autoneg != AUTONEG_ENABLE)
858 + return -EINVAL;
859 +
860 + mutex_lock(&pl->config_mutex);
861 + ret = __phylink_ethtool_ksettings_set(pl, kset);
862 + mutex_unlock(&pl->config_mutex);
863 +
864 + return ret;
865 +}
866 +EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
867 +
868 +/* This emulates MII registers for a fixed-mode phy operating as per the
869 + * passed in state. "aneg" defines if we report negotiation is possible.
870 + *
871 + * FIXME: should deal with negotiation state too.
872 + */
873 +static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
874 + struct phylink_link_state *state, bool aneg)
875 +{
876 + struct fixed_phy_status fs;
877 + int val;
878 +
879 + fs.link = state->link;
880 + fs.speed = state->speed;
881 + fs.duplex = state->duplex;
882 + fs.pause = state->pause & MLO_PAUSE_SYM;
883 + fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
884 +
885 + val = swphy_read_reg(reg, &fs);
886 + if (reg == MII_BMSR) {
887 + if (!state->an_complete)
888 + val &= ~BMSR_ANEGCOMPLETE;
889 + if (!aneg)
890 + val &= ~BMSR_ANEGCAPABLE;
891 + }
892 + return val;
893 +}
894 +
895 +static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
896 + unsigned int reg)
897 +{
898 + struct phylink_link_state state;
899 + int val = 0xffff;
900 +
901 + /* PHYs only exist for MLO_AN_PHY and MLO_AN_SGMII */
902 + if (pl->phydev)
903 + return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg);
904 +
905 + switch (pl->link_an_mode) {
906 + case MLO_AN_FIXED:
907 + if (phy_id == 0) {
908 + phylink_get_fixed_state(pl, &state);
909 + val = phylink_mii_emul_read(pl->netdev, reg, &state,
910 + true);
911 + }
912 + break;
913 +
914 + case MLO_AN_PHY:
915 + return -EOPNOTSUPP;
916 +
917 + case MLO_AN_SGMII:
918 + /* No phy, fall through to 8023z method */
919 + case MLO_AN_8023Z:
920 + if (phy_id == 0) {
921 + val = phylink_get_mac_state(pl, &state);
922 + if (val < 0)
923 + return val;
924 +
925 + val = phylink_mii_emul_read(pl->netdev, reg, &state,
926 + true);
927 + }
928 + break;
929 + }
930 +
931 + return val & 0xffff;
932 +}
933 +
934 +static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
935 + unsigned int reg, unsigned int val)
936 +{
937 + /* PHYs only exist for MLO_AN_PHY and MLO_AN_SGMII */
938 + if (pl->phydev) {
939 + mdiobus_write(pl->phydev->mdio.bus, phy_id, reg, val);
940 + return 0;
941 + }
942 +
943 + switch (pl->link_an_mode) {
944 + case MLO_AN_FIXED:
945 + break;
946 +
947 + case MLO_AN_PHY:
948 + return -EOPNOTSUPP;
949 +
950 + case MLO_AN_SGMII:
951 + /* No phy, fall through to 8023z method */
952 + case MLO_AN_8023Z:
953 + break;
954 + }
955 +
956 + return 0;
957 +}
958 +
959 +int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
960 +{
961 + struct mii_ioctl_data *mii_data = if_mii(ifr);
962 + int val, ret;
963 +
964 + mutex_lock(&pl->config_mutex);
965 +
966 + switch (cmd) {
967 + case SIOCGMIIPHY:
968 + mii_data->phy_id = pl->phydev ? pl->phydev->mdio.addr : 0;
969 + /* fallthrough */
970 +
971 + case SIOCGMIIREG:
972 + val = phylink_mii_read(pl, mii_data->phy_id, mii_data->reg_num);
973 + if (val < 0) {
974 + ret = val;
975 + } else {
976 + mii_data->val_out = val;
977 + ret = 0;
978 + }
979 + break;
980 +
981 + case SIOCSMIIREG:
982 + ret = phylink_mii_write(pl, mii_data->phy_id, mii_data->reg_num,
983 + mii_data->val_in);
984 + break;
985 +
986 + default:
987 + ret = -EOPNOTSUPP;
988 + if (pl->phydev)
989 + ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
990 + break;
991 + }
992 +
993 + mutex_unlock(&pl->config_mutex);
994 +
995 + return ret;
996 +}
997 +EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
998 +
999 +MODULE_LICENSE("GPL");
1000 --- a/include/linux/phy.h
1001 +++ b/include/linux/phy.h
1002 @@ -149,6 +149,7 @@ static inline const char *phy_modes(phy_
1003 #define MII_ADDR_C45 (1<<30)
1004
1005 struct device;
1006 +struct phylink;
1007 struct sk_buff;
1008
1009 /*
1010 @@ -421,6 +422,7 @@ struct phy_device {
1011
1012 struct mutex lock;
1013
1014 + struct phylink *phylink;
1015 struct net_device *attached_dev;
1016
1017 u8 mdix;
1018 --- /dev/null
1019 +++ b/include/linux/phylink.h
1020 @@ -0,0 +1,100 @@
1021 +#ifndef NETDEV_PCS_H
1022 +#define NETDEV_PCS_H
1023 +
1024 +#include <linux/phy.h>
1025 +#include <linux/spinlock.h>
1026 +#include <linux/workqueue.h>
1027 +
1028 +struct device_node;
1029 +struct ethtool_cmd;
1030 +struct net_device;
1031 +
1032 +enum {
1033 + MLO_PAUSE_NONE,
1034 + MLO_PAUSE_ASYM = BIT(0),
1035 + MLO_PAUSE_SYM = BIT(1),
1036 +
1037 + MLO_AN_PHY = 0,
1038 + MLO_AN_FIXED,
1039 + MLO_AN_SGMII,
1040 + MLO_AN_8023Z,
1041 +};
1042 +
1043 +struct phylink_link_state {
1044 + __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
1045 + __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
1046 + int speed;
1047 + int duplex;
1048 + int pause;
1049 + unsigned int link:1;
1050 + unsigned int an_enabled:1;
1051 + unsigned int an_complete:1;
1052 +};
1053 +
1054 +struct phylink_mac_ops {
1055 + /**
1056 + * @validate_support:
1057 + *
1058 + * Validate and update the support mask provided by a PHY or
1059 + * module. Unsupported link modes should be cleared by the
1060 + * MAC.
1061 + *
1062 + * Note: the PHY may be able to transform from one connection
1063 + * technology to another, so, eg, don't clear 1000base-X just
1064 + * because the MAC is unable to support it. This is more about
1065 + * clearing unsupported speeds and duplex settings.
1066 + */
1067 + void (*validate_support)(struct net_device *, unsigned int mode,
1068 + unsigned long *support);
1069 +
1070 + /**
1071 + * @validate_advert:
1072 + *
1073 + * Validate and update the advertisment mask, clearing bits that
1074 + * can not be advertised in the chosen mode or with each other.
1075 + */
1076 + void (*validate_advert)(struct net_device *, unsigned int mode,
1077 + const unsigned long *support,
1078 + unsigned long *advert);
1079 +
1080 + /* Read the current link state from the hardware */
1081 + int (*mac_link_state)(struct net_device *, struct phylink_link_state *);
1082 +
1083 + /* Configure the MAC */
1084 + void (*mac_config)(struct net_device *, unsigned int mode,
1085 + const struct phylink_link_state *);
1086 + void (*mac_an_restart)(struct net_device *, unsigned int mode);
1087 +
1088 + void (*mac_link_down)(struct net_device *, unsigned int mode);
1089 + void (*mac_link_up)(struct net_device *, unsigned int mode);
1090 +};
1091 +
1092 +struct phylink *phylink_create(struct net_device *, struct device_node *,
1093 + phy_interface_t iface, const struct phylink_mac_ops *ops);
1094 +void phylink_destroy(struct phylink *);
1095 +
1096 +int phylink_connect_phy(struct phylink *, struct phy_device *);
1097 +int phylink_of_phy_connect(struct phylink *, struct device_node *);
1098 +void phylink_disconnect_phy(struct phylink *);
1099 +
1100 +void phylink_mac_change(struct phylink *, bool up);
1101 +
1102 +void phylink_start(struct phylink *);
1103 +void phylink_stop(struct phylink *);
1104 +
1105 +int phylink_ethtool_ksettings_get(struct phylink *,
1106 + struct ethtool_link_ksettings *);
1107 +int phylink_ethtool_ksettings_set(struct phylink *,
1108 + const struct ethtool_link_ksettings *);
1109 +int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
1110 +
1111 +#define phylink_zero(bm) \
1112 + bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS)
1113 +#define __phylink_do_bit(op, bm, mode) \
1114 + op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm)
1115 +
1116 +#define phylink_set(bm, mode) __phylink_do_bit(__set_bit, bm, mode)
1117 +#define phylink_clear(bm, mode) __phylink_do_bit(__clear_bit, bm, mode)
1118 +#define phylink_test(bm, mode) __phylink_do_bit(test_bit, bm, mode)
1119 +
1120 +#endif