1 From c6de6de7d3df13822872ac756eebe868d236297a Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@arm.linux.org.uk>
3 Date: Tue, 22 Sep 2015 20:52:18 +0100
4 Subject: [PATCH 718/744] phylink: add phylink infrastructure
6 The link between the ethernet MAC and its PHY has become more complex
7 as the interface evolves. This is especially true with serdes links,
8 where the part of the PHY is effectively integrated into the MAC.
10 Serdes links can be connected to a variety of devices, including SFF
11 modules soldered down onto the board with the MAC, a SFP cage with
12 a hotpluggable SFP module which may contain a PHY or directly modulate
13 the serdes signals onto optical media with or without a PHY, or even
14 a classical PHY connection.
16 Moreover, the negotiation information on serdes links comes in two
17 varieties - SGMII mode, where the PHY provides its speed/duplex/flow
18 control information to the MAC, and 1000base-X mode where both ends
19 exchange their abilities and each resolve the link capabilities.
21 This means we need a more flexible means to support these arrangements,
22 particularly with the hotpluggable nature of SFP, where the PHY can
23 be attached or detached after the network device has been brought up.
25 Ethtool information can come from multiple sources:
26 - we may have a PHY operating in either SGMII or 1000base-X mode, in
27 which case we take ethtool/mii data directly from the PHY.
28 - we may have a optical SFP module without a PHY, with the MAC
29 operating in 1000base-X mode - the ethtool/mii data needs to come
31 - we may have a copper SFP module with a PHY whic can't be accessed,
32 which means we need to take ethtool/mii data from the MAC.
34 Phylink aims to solve this by providing an intermediary between the
35 MAC and PHY, providing a safe way for PHYs to be hotplugged, and
36 allowing a SFP driver to reconfigure the serdes connection.
38 Phylink also takes over support of fixed link connections, where
39 the speed/duplex/flow control are fixed, but link status may be
40 controlled by a GPIO signal. By avoiding the fixed-phy implementation,
41 phylink can provide a faster response to link events: fixed-phy has
42 to wait for phylib to operate its state machine, which can take
43 several seconds. In comparison, phylink takes milliseconds.
45 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
47 drivers/net/phy/Kconfig | 10 +
48 drivers/net/phy/Makefile | 1 +
49 drivers/net/phy/phy_device.c | 1 +
50 drivers/net/phy/phylink.c | 816 +++++++++++++++++++++++++++++++++++++++++++
51 include/linux/phy.h | 2 +
52 include/linux/phylink.h | 70 ++++
53 6 files changed, 900 insertions(+)
54 create mode 100644 drivers/net/phy/phylink.c
55 create mode 100644 include/linux/phylink.h
57 --- a/drivers/net/phy/Kconfig
58 +++ b/drivers/net/phy/Kconfig
59 @@ -10,6 +10,16 @@ menuconfig PHYLIB
60 devices. This option provides infrastructure for
65 + depends on NETDEVICES
69 + PHYlink models the link between the PHY and MAC, allowing fixed
70 + configuration links, PHYs, and Serdes links with MAC level
71 + autonegotiation modes.
76 --- a/drivers/net/phy/Makefile
77 +++ b/drivers/net/phy/Makefile
78 @@ -5,6 +5,7 @@ libphy-$(CONFIG_SWPHY) += swphy.o
80 obj-$(CONFIG_MDIO_BOARDINFO) += mdio-boardinfo.o
82 +obj-$(CONFIG_PHYLINK) += phylink.o
83 obj-$(CONFIG_PHYLIB) += libphy.o
84 obj-$(CONFIG_SWCONFIG) += swconfig.o
85 obj-$(CONFIG_AQUANTIA_PHY) += aquantia.o
86 --- a/drivers/net/phy/phy_device.c
87 +++ b/drivers/net/phy/phy_device.c
88 @@ -754,6 +754,7 @@ void phy_detach(struct phy_device *phyde
89 phydev->attached_dev->phydev = NULL;
90 phydev->attached_dev = NULL;
92 + phydev->phylink = NULL;
94 /* If the device had no specific driver before (i.e. - it
95 * was using the generic driver), we unbind the device
97 +++ b/drivers/net/phy/phylink.c
100 + * phylink models the MAC to optional PHY connection, supporting
101 + * technologies such as SFP cages where the PHY is hot-pluggable.
103 + * Copyright (C) 2015 Russell King
105 + * This program is free software; you can redistribute it and/or modify
106 + * it under the terms of the GNU General Public License version 2 as
107 + * published by the Free Software Foundation.
109 +#include <linux/ethtool.h>
110 +#include <linux/export.h>
111 +#include <linux/gpio/consumer.h>
112 +#include <linux/netdevice.h>
113 +#include <linux/of.h>
114 +#include <linux/of_mdio.h>
115 +#include <linux/phy.h>
116 +#include <linux/phy_fixed.h>
117 +#include <linux/phylink.h>
118 +#include <linux/spinlock.h>
119 +#include <linux/workqueue.h>
123 +#define SUPPORTED_INTERFACES \
124 + (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
125 + SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
126 +#define ADVERTISED_INTERFACES \
127 + (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
128 + ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
131 + PHYLINK_DISABLE_STOPPED,
135 + struct net_device *netdev;
136 + const struct phylink_mac_ops *ops;
137 + struct mutex config_mutex;
139 + unsigned long phylink_disable_state; /* bitmask of disables */
140 + struct phy_device *phydev;
141 + phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
142 + u8 link_an_mode; /* MLO_AN_xxx */
143 + u8 link_port; /* The current non-phy ethtool port */
144 + u32 link_port_support; /* SUPPORTED_xxx ethtool for ports */
146 + /* The link configuration settings */
147 + struct phylink_link_state link_config;
148 + struct gpio_desc *link_gpio;
150 + struct mutex state_mutex; /* may be taken within config_mutex */
151 + struct phylink_link_state phy_state;
152 + struct work_struct resolve;
157 +static const char *phylink_an_mode_str(unsigned int mode)
159 + static const char *modestr[] = {
160 + [MLO_AN_PHY] = "phy",
161 + [MLO_AN_FIXED] = "fixed",
162 + [MLO_AN_SGMII] = "SGMII",
163 + [MLO_AN_8023Z] = "802.3z",
166 + return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
169 +static int phylink_parse_fixedlink(struct phylink *pl, struct device_node *np)
171 + struct device_node *fixed_node;
174 + fixed_node = of_get_child_by_name(np, "fixed-link");
176 + struct gpio_desc *desc;
179 + ret = of_property_read_u32(fixed_node, "speed", &speed);
181 + pl->link_an_mode = MLO_AN_FIXED;
182 + pl->link_config.link = 1;
183 + pl->link_config.an_complete = 1;
184 + pl->link_config.speed = speed;
185 + pl->link_config.duplex = DUPLEX_HALF;
186 + pl->link_config.pause = MLO_PAUSE_NONE;
188 + if (of_property_read_bool(fixed_node, "full-duplex"))
189 + pl->link_config.duplex = DUPLEX_FULL;
190 + if (of_property_read_bool(fixed_node, "pause"))
191 + pl->link_config.pause |= MLO_PAUSE_SYM;
192 + if (of_property_read_bool(fixed_node, "asym-pause"))
193 + pl->link_config.pause |= MLO_PAUSE_ASYM;
196 + desc = fwnode_get_named_gpiod(&fixed_node->fwnode,
200 + pl->link_gpio = desc;
201 + else if (desc == ERR_PTR(-EPROBE_DEFER))
202 + ret = -EPROBE_DEFER;
204 + of_node_put(fixed_node);
206 + const __be32 *fixed_prop;
208 + fixed_prop = of_get_property(np, "fixed-link", &len);
209 + if (fixed_prop && len == 5 * sizeof(*fixed_prop)) {
210 + pl->link_config.duplex = be32_to_cpu(fixed_prop[1]) ?
211 + DUPLEX_FULL : DUPLEX_HALF;
212 + pl->link_config.speed = be32_to_cpu(fixed_prop[2]);
213 + pl->link_config.pause = MLO_PAUSE_NONE;
214 + if (be32_to_cpu(fixed_prop[3]))
215 + pl->link_config.pause |= MLO_PAUSE_SYM;
216 + if (be32_to_cpu(fixed_prop[4]))
217 + pl->link_config.pause |= MLO_PAUSE_ASYM;
219 + pl->link_an_mode = MLO_AN_FIXED;
224 + if (pl->link_an_mode == MLO_AN_FIXED) {
225 + /* Generate the supported/advertising masks */
226 + if (pl->link_config.pause & MLO_PAUSE_SYM) {
227 + pl->link_config.supported |= SUPPORTED_Pause;
228 + pl->link_config.advertising |= ADVERTISED_Pause;
230 + if (pl->link_config.pause & MLO_PAUSE_ASYM) {
231 + pl->link_config.supported |= SUPPORTED_Asym_Pause;
232 + pl->link_config.advertising |= ADVERTISED_Asym_Pause;
235 + if (pl->link_config.speed > SPEED_1000 &&
236 + pl->link_config.duplex != DUPLEX_FULL)
237 + netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
238 + pl->link_config.speed);
241 + pl->link_config.supported |= pl->link_config.duplex ? \
242 + SUPPORTED_##spd##_Full : SUPPORTED_##spd##_Half
244 + pl->link_config.advertising |= pl->link_config.duplex ? \
245 + ADVERTISED_##spd##_Full : ADVERTISED_##spd##_Half
246 +#define C(spd, tech) \
251 + switch (pl->link_config.speed) {
257 +#define S(spd) pl->link_config.supported |= SUPPORTED_##spd##_Full
258 +#define A(spd) pl->link_config.advertising |= ADVERTISED_##spd##_Full
269 +static int phylink_parse_managed(struct phylink *pl, struct device_node *np)
271 + const char *managed;
273 + if (of_property_read_string(np, "managed", &managed) == 0 &&
274 + strcmp(managed, "in-band-status") == 0) {
275 + if (pl->link_an_mode == MLO_AN_FIXED) {
276 + netdev_err(pl->netdev,
277 + "can't use both fixed-link and in-band-status\n");
280 + pl->link_an_mode = MLO_AN_SGMII;
281 + pl->link_config.an_enabled = true;
288 +static int phylink_get_support(struct phylink *pl, unsigned int mode)
290 + struct phylink_link_state state = pl->link_config;
293 + ret = pl->ops->mac_get_support(pl->netdev, mode, &state);
295 + pl->link_an_mode = mode;
296 + pl->link_config = state;
302 +static void phylink_mac_config(struct phylink *pl,
303 + const struct phylink_link_state *state)
305 + pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
308 +static void phylink_mac_an_restart(struct phylink *pl)
310 + if (pl->link_config.an_enabled)
311 + pl->ops->mac_an_restart(pl->netdev, pl->link_an_mode);
314 +static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
316 + struct net_device *ndev = pl->netdev;
318 + state->supported = pl->link_config.supported;
319 + state->advertising = pl->link_config.advertising;
320 + state->an_enabled = pl->link_config.an_enabled;
324 + return pl->ops->mac_link_state(ndev, state);
327 +/* The fixed state is... fixed except for the link state,
328 + * which may be determined by a GPIO.
330 +static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
332 + *state = pl->link_config;
334 + state->link = !!gpiod_get_value(pl->link_gpio);
337 +extern const char *phy_speed_to_str(int speed);
339 +static void phylink_resolve(struct work_struct *w)
341 + struct phylink *pl = container_of(w, struct phylink, resolve);
342 + struct phylink_link_state link_state;
343 + struct net_device *ndev = pl->netdev;
345 + mutex_lock(&pl->state_mutex);
346 + if (pl->phylink_disable_state) {
347 + link_state.link = false;
349 + switch (pl->link_an_mode) {
351 + link_state = pl->phy_state;
355 + phylink_get_fixed_state(pl, &link_state);
359 + phylink_get_mac_state(pl, &link_state);
361 + link_state.link = link_state.link &&
362 + pl->phy_state.link;
366 + phylink_get_mac_state(pl, &link_state);
371 + if (link_state.link != netif_carrier_ok(ndev)) {
372 + if (!link_state.link) {
373 + netif_carrier_off(ndev);
374 + pl->ops->mac_link_down(ndev, pl->link_an_mode);
375 + netdev_info(ndev, "Link is Down\n");
377 + /* If we have a PHY, we need the MAC updated with
378 + * the current link parameters (eg, in SGMII mode,
379 + * with flow control status.)
382 + phylink_mac_config(pl, &link_state);
384 + pl->ops->mac_link_up(ndev, pl->link_an_mode);
386 + netif_carrier_on(ndev);
389 + "Link is Up - %s/%s - flow control %s\n",
390 + phy_speed_to_str(link_state.speed),
391 + link_state.duplex ? "Full" : "Half",
392 + link_state.pause ? "rx/tx" : "off");
395 + mutex_unlock(&pl->state_mutex);
398 +static void phylink_run_resolve(struct phylink *pl)
400 + if (!pl->phylink_disable_state)
401 + queue_work(system_power_efficient_wq, &pl->resolve);
404 +struct phylink *phylink_create(struct net_device *ndev, struct device_node *np,
405 + phy_interface_t iface, const struct phylink_mac_ops *ops)
407 + struct phylink *pl;
410 + pl = kzalloc(sizeof(*pl), GFP_KERNEL);
412 + return ERR_PTR(-ENOMEM);
414 + mutex_init(&pl->state_mutex);
415 + mutex_init(&pl->config_mutex);
416 + INIT_WORK(&pl->resolve, phylink_resolve);
418 + pl->link_interface = iface;
419 + pl->link_port_support = SUPPORTED_MII;
420 + pl->link_port = PORT_MII;
422 + __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
424 + ret = phylink_parse_fixedlink(pl, np);
427 + return ERR_PTR(ret);
430 + ret = phylink_parse_managed(pl, np);
433 + return ERR_PTR(ret);
436 + ret = phylink_get_support(pl, pl->link_an_mode);
439 + return ERR_PTR(ret);
444 +EXPORT_SYMBOL_GPL(phylink_create);
446 +void phylink_destroy(struct phylink *pl)
448 + cancel_work_sync(&pl->resolve);
451 +EXPORT_SYMBOL_GPL(phylink_destroy);
453 +void phylink_phy_change(struct phy_device *phy, bool up, bool do_carrier)
455 + struct phylink *pl = phy->phylink;
457 + mutex_lock(&pl->state_mutex);
458 + pl->phy_state.speed = phy->speed;
459 + pl->phy_state.duplex = phy->duplex;
460 + pl->phy_state.pause = MLO_PAUSE_NONE;
462 + pl->phy_state.pause |= MLO_PAUSE_SYM;
463 + if (phy->asym_pause)
464 + pl->phy_state.pause |= MLO_PAUSE_ASYM;
465 + pl->phy_state.link = up;
466 + mutex_unlock(&pl->state_mutex);
468 + phylink_run_resolve(pl);
470 + netdev_dbg(pl->netdev, "phy link %s\n", up ? "up" : "down");
473 +static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
475 + mutex_lock(&pl->config_mutex);
477 + phy->phy_link_change = phylink_phy_change;
479 + netdev_info(pl->netdev,
480 + "PHY [%s] driver [%s]\n", dev_name(&phy->dev),
483 + mutex_lock(&pl->state_mutex);
486 + /* Restrict the phy advertisment to the union of the PHY and
487 + * MAC-level advert.
489 + phy->advertising &= ADVERTISED_INTERFACES |
490 + pl->link_config.advertising;
491 + mutex_unlock(&pl->state_mutex);
493 + phy_start_machine(phy);
495 + phy_start_interrupts(phy);
497 + mutex_unlock(&pl->config_mutex);
502 +int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
506 + ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
510 + ret = phylink_bringup_phy(pl, phy);
516 +EXPORT_SYMBOL_GPL(phylink_connect_phy);
518 +int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn)
520 + struct device_node *phy_node;
521 + struct phy_device *phy_dev;
524 + /* Fixed links are handled without needing a PHY */
525 + if (pl->link_an_mode == MLO_AN_FIXED)
528 + phy_node = of_parse_phandle(dn, "phy-handle", 0);
530 + phy_node = of_parse_phandle(dn, "phy", 0);
532 + phy_node = of_parse_phandle(dn, "phy-device", 0);
535 + if (pl->link_an_mode == MLO_AN_PHY) {
536 + netdev_err(pl->netdev, "unable to find PHY node\n");
542 + phy_dev = of_phy_attach(pl->netdev, phy_node, 0, pl->link_interface);
543 + /* We're done with the phy_node handle */
544 + of_node_put(phy_node);
549 + ret = phylink_bringup_phy(pl, phy_dev);
551 + phy_detach(phy_dev);
555 +EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
557 +void phylink_disconnect_phy(struct phylink *pl)
559 + struct phy_device *phy;
561 + mutex_lock(&pl->config_mutex);
564 + mutex_lock(&pl->state_mutex);
566 + mutex_unlock(&pl->state_mutex);
567 + flush_work(&pl->resolve);
570 + phy_disconnect(phy);
572 + mutex_unlock(&pl->config_mutex);
574 +EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
576 +void phylink_mac_change(struct phylink *pl, bool up)
578 + phylink_run_resolve(pl);
579 + netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
581 +EXPORT_SYMBOL_GPL(phylink_mac_change);
583 +void phylink_start(struct phylink *pl)
585 + mutex_lock(&pl->config_mutex);
587 + netdev_info(pl->netdev, "configuring for %s link mode\n",
588 + phylink_an_mode_str(pl->link_an_mode));
590 + /* Apply the link configuration to the MAC when starting. This allows
591 + * a fixed-link to start with the correct parameters, and also
592 + * ensures that we set the appropriate advertisment for Serdes links.
594 + phylink_mac_config(pl, &pl->link_config);
596 + clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
597 + phylink_run_resolve(pl);
600 + phy_start(pl->phydev);
602 + mutex_unlock(&pl->config_mutex);
604 +EXPORT_SYMBOL_GPL(phylink_start);
606 +void phylink_stop(struct phylink *pl)
608 + mutex_lock(&pl->config_mutex);
611 + phy_stop(pl->phydev);
613 + set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
614 + flush_work(&pl->resolve);
616 + pl->mac_link_up = false;
618 + mutex_unlock(&pl->config_mutex);
620 +EXPORT_SYMBOL_GPL(phylink_stop);
622 +static void phylink_get_ethtool(const struct phylink_link_state *state,
623 + struct ethtool_cmd *cmd)
625 + cmd->supported &= SUPPORTED_INTERFACES;
626 + cmd->supported |= state->supported;
627 + cmd->advertising &= ADVERTISED_INTERFACES;
628 + cmd->advertising |= state->advertising;
629 + ethtool_cmd_speed_set(cmd, state->speed);
630 + cmd->duplex = state->duplex;
632 + cmd->autoneg = state->an_enabled ? AUTONEG_ENABLE : AUTONEG_DISABLE;
635 +static int phylink_ethtool_gset(struct phylink *pl, struct ethtool_cmd *cmd)
637 + struct phylink_link_state link_state;
641 + ret = phy_ethtool_gset(pl->phydev, cmd);
645 + cmd->supported &= SUPPORTED_INTERFACES |
646 + pl->link_config.supported;
648 + cmd->supported = pl->link_port_support;
649 + cmd->transceiver = XCVR_EXTERNAL;
650 + cmd->port = pl->link_port;
653 + switch (pl->link_an_mode) {
655 + /* We are using fixed settings. Report these as the
656 + * current link settings - and note that these also
657 + * represent the supported speeds/duplex/pause modes.
659 + phylink_get_fixed_state(pl, &link_state);
660 + phylink_get_ethtool(&link_state, cmd);
664 + /* If there is a phy attached, then use the reported
665 + * settings from the phy with no modification.
671 + phylink_get_mac_state(pl, &link_state);
673 + /* The MAC is reporting the link results from its own PCS
674 + * layer via in-band status. Report these as the current
677 + phylink_get_ethtool(&link_state, cmd);
684 +int phylink_ethtool_get_settings(struct phylink *pl, struct ethtool_cmd *cmd)
688 + mutex_lock(&pl->config_mutex);
689 + ret = phylink_ethtool_gset(pl, cmd);
690 + mutex_unlock(&pl->config_mutex);
694 +EXPORT_SYMBOL_GPL(phylink_ethtool_get_settings);
696 +static int phylink_ethtool_sset(struct phylink *pl, struct ethtool_cmd *cmd)
701 + /* Calculate the union of the MAC support and attached phy support */
702 + supported = pl->link_config.supported;
704 + supported &= pl->phydev->supported;
706 + /* Mask out unsupported advertisments */
707 + cmd->advertising &= supported;
709 + /* FIXME: should we reject autoneg if phy/mac does not support it? */
711 + if (cmd->autoneg == AUTONEG_DISABLE) {
712 + /* Autonegotiation disabled, validate speed and duplex */
713 + if (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL)
716 + /* FIXME: validate speed/duplex against supported */
718 + cmd->advertising &= ~ADVERTISED_Autoneg;
720 + /* Autonegotiation enabled, validate advertisment */
721 + /* FIXME: shouldn't we ensure there's some duplex/speeds set */
722 + if (cmd->advertising == 0)
725 + cmd->advertising |= ADVERTISED_Autoneg;
728 + /* If we have a fixed link (as specified by firmware), refuse
729 + * to enable autonegotiation, or change link parameters.
731 + if (pl->link_an_mode == MLO_AN_FIXED) {
732 + if (cmd->autoneg != AUTONEG_DISABLE ||
733 + ethtool_cmd_speed(cmd) != pl->link_config.speed ||
734 + cmd->duplex != pl->link_config.duplex)
738 + /* If we have a PHY, configure the phy */
740 + ret = phy_ethtool_sset(pl->phydev, cmd);
745 + mutex_lock(&pl->state_mutex);
746 + /* Configure the MAC to match the new settings */
747 + pl->link_config.advertising = cmd->advertising;
748 + pl->link_config.speed = cmd->speed;
749 + pl->link_config.duplex = cmd->duplex;
750 + pl->link_config.an_enabled = cmd->autoneg != AUTONEG_DISABLE;
752 + phylink_mac_config(pl, &pl->link_config);
753 + phylink_mac_an_restart(pl);
754 + mutex_unlock(&pl->state_mutex);
759 +int phylink_ethtool_set_settings(struct phylink *pl, struct ethtool_cmd *cmd)
763 + if (cmd->autoneg != AUTONEG_DISABLE && cmd->autoneg != AUTONEG_ENABLE)
766 + mutex_lock(&pl->config_mutex);
767 + ret = phylink_ethtool_sset(pl, cmd);
768 + mutex_unlock(&pl->config_mutex);
772 +EXPORT_SYMBOL_GPL(phylink_ethtool_set_settings);
774 +/* This emulates MII registers for a fixed-mode phy operating as per the
775 + * passed in state. "aneg" defines if we report negotiation is possible.
777 + * FIXME: should deal with negotiation state too.
779 +static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
780 + struct phylink_link_state *state, bool aneg)
782 + struct fixed_phy_status fs;
785 + fs.link = state->link;
786 + fs.speed = state->speed;
787 + fs.duplex = state->duplex;
788 + fs.pause = state->pause & MLO_PAUSE_SYM;
789 + fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
791 + val = swphy_read_reg(reg, &fs);
792 + if (reg == MII_BMSR) {
793 + if (!state->an_complete)
794 + val &= ~BMSR_ANEGCOMPLETE;
796 + val &= ~BMSR_ANEGCAPABLE;
801 +static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
804 + struct phylink_link_state state;
807 + if (pl->phydev && pl->phydev->addr != phy_id)
808 + return mdiobus_read(pl->phydev->bus, phy_id, reg);
810 + if (!pl->phydev && phy_id != 0)
813 + switch (pl->link_an_mode) {
815 + phylink_get_fixed_state(pl, &state);
816 + val = phylink_mii_emul_read(pl->netdev, reg, &state, true);
820 + val = mdiobus_read(pl->phydev->bus, phy_id, reg);
825 + val = mdiobus_read(pl->phydev->bus,
826 + pl->phydev->addr, reg);
829 + /* No phy, fall through to reading the MAC end */
831 + val = phylink_get_mac_state(pl, &state);
835 + val = phylink_mii_emul_read(pl->netdev, reg, &state, true);
839 + return val & 0xffff;
842 +static void phylink_mii_write(struct phylink *pl, unsigned int phy_id,
843 + unsigned int reg, unsigned int val)
845 + if (pl->phydev && pl->phydev->addr != phy_id) {
846 + mdiobus_write(pl->phydev->bus, phy_id, reg, val);
850 + if (!pl->phydev && phy_id != 0)
853 + switch (pl->link_an_mode) {
858 + mdiobus_write(pl->phydev->bus, pl->phydev->addr,
864 + mdiobus_write(pl->phydev->bus, phy_id, reg, val);
867 + /* No phy, fall through to reading the MAC end */
873 +int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
875 + struct mii_ioctl_data *mii_data = if_mii(ifr);
878 + mutex_lock(&pl->config_mutex);
882 + mii_data->phy_id = pl->phydev ? pl->phydev->addr : 0;
886 + val = phylink_mii_read(pl, mii_data->phy_id, mii_data->reg_num);
890 + mii_data->val_out = val;
896 + phylink_mii_write(pl, mii_data->phy_id, mii_data->reg_num,
904 + ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
908 + mutex_unlock(&pl->config_mutex);
912 +EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
914 +MODULE_LICENSE("GPL");
915 --- a/include/linux/phy.h
916 +++ b/include/linux/phy.h
917 @@ -147,6 +147,7 @@ static inline const char *phy_modes(phy_
918 #define MII_ADDR_C45 (1<<30)
925 @@ -429,6 +430,7 @@ struct phy_device {
929 + struct phylink *phylink;
930 struct net_device *attached_dev;
934 +++ b/include/linux/phylink.h
936 +#ifndef NETDEV_PCS_H
937 +#define NETDEV_PCS_H
939 +#include <linux/phy.h>
940 +#include <linux/spinlock.h>
941 +#include <linux/workqueue.h>
949 + MLO_PAUSE_ASYM = BIT(0),
950 + MLO_PAUSE_SYM = BIT(1),
958 +struct phylink_link_state {
961 + u32 lp_advertising;
965 + unsigned int link:1;
966 + unsigned int sync:1;
967 + unsigned int an_enabled:1;
968 + unsigned int an_complete:1;
971 +struct phylink_mac_ops {
972 + /* Get the ethtool supported mask for the indicated mode */
973 + int (*mac_get_support)(struct net_device *, unsigned int mode,
974 + struct phylink_link_state *);
976 + /* Read the current link state from the hardware */
977 + int (*mac_link_state)(struct net_device *, struct phylink_link_state *);
979 + /* Configure the MAC */
980 + void (*mac_config)(struct net_device *, unsigned int mode,
981 + const struct phylink_link_state *);
982 + void (*mac_an_restart)(struct net_device *, unsigned int mode);
984 + void (*mac_link_down)(struct net_device *, unsigned int mode);
985 + void (*mac_link_up)(struct net_device *, unsigned int mode);
988 +struct phylink *phylink_create(struct net_device *, struct device_node *,
989 + phy_interface_t iface, const struct phylink_mac_ops *ops);
990 +void phylink_destroy(struct phylink *);
992 +int phylink_connect_phy(struct phylink *, struct phy_device *);
993 +int phylink_of_phy_connect(struct phylink *, struct device_node *);
994 +void phylink_disconnect_phy(struct phylink *);
996 +void phylink_mac_change(struct phylink *, bool up);
998 +void phylink_start(struct phylink *);
999 +void phylink_stop(struct phylink *);
1001 +int phylink_ethtool_get_settings(struct phylink *, struct ethtool_cmd *);
1002 +int phylink_ethtool_set_settings(struct phylink *, struct ethtool_cmd *);
1003 +int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);