162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * phylink models the MAC to optional PHY connection, supporting
462306a36Sopenharmony_ci * technologies such as SFP cages where the PHY is hot-pluggable.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 2015 Russell King
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci#include <linux/acpi.h>
962306a36Sopenharmony_ci#include <linux/ethtool.h>
1062306a36Sopenharmony_ci#include <linux/export.h>
1162306a36Sopenharmony_ci#include <linux/gpio/consumer.h>
1262306a36Sopenharmony_ci#include <linux/netdevice.h>
1362306a36Sopenharmony_ci#include <linux/of.h>
1462306a36Sopenharmony_ci#include <linux/of_mdio.h>
1562306a36Sopenharmony_ci#include <linux/phy.h>
1662306a36Sopenharmony_ci#include <linux/phy_fixed.h>
1762306a36Sopenharmony_ci#include <linux/phylink.h>
1862306a36Sopenharmony_ci#include <linux/rtnetlink.h>
1962306a36Sopenharmony_ci#include <linux/spinlock.h>
2062306a36Sopenharmony_ci#include <linux/timer.h>
2162306a36Sopenharmony_ci#include <linux/workqueue.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#include "sfp.h"
2462306a36Sopenharmony_ci#include "swphy.h"
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define SUPPORTED_INTERFACES \
2762306a36Sopenharmony_ci	(SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
2862306a36Sopenharmony_ci	 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
2962306a36Sopenharmony_ci#define ADVERTISED_INTERFACES \
3062306a36Sopenharmony_ci	(ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
3162306a36Sopenharmony_ci	 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cienum {
3462306a36Sopenharmony_ci	PHYLINK_DISABLE_STOPPED,
3562306a36Sopenharmony_ci	PHYLINK_DISABLE_LINK,
3662306a36Sopenharmony_ci	PHYLINK_DISABLE_MAC_WOL,
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	PCS_STATE_DOWN = 0,
3962306a36Sopenharmony_ci	PCS_STATE_STARTING,
4062306a36Sopenharmony_ci	PCS_STATE_STARTED,
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/**
4462306a36Sopenharmony_ci * struct phylink - internal data type for phylink
4562306a36Sopenharmony_ci */
4662306a36Sopenharmony_cistruct phylink {
4762306a36Sopenharmony_ci	/* private: */
4862306a36Sopenharmony_ci	struct net_device *netdev;
4962306a36Sopenharmony_ci	const struct phylink_mac_ops *mac_ops;
5062306a36Sopenharmony_ci	struct phylink_config *config;
5162306a36Sopenharmony_ci	struct phylink_pcs *pcs;
5262306a36Sopenharmony_ci	struct device *dev;
5362306a36Sopenharmony_ci	unsigned int old_link_state:1;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	unsigned long phylink_disable_state; /* bitmask of disables */
5662306a36Sopenharmony_ci	struct phy_device *phydev;
5762306a36Sopenharmony_ci	phy_interface_t link_interface;	/* PHY_INTERFACE_xxx */
5862306a36Sopenharmony_ci	u8 cfg_link_an_mode;		/* MLO_AN_xxx */
5962306a36Sopenharmony_ci	u8 cur_link_an_mode;
6062306a36Sopenharmony_ci	u8 link_port;			/* The current non-phy ethtool port */
6162306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	/* The link configuration settings */
6462306a36Sopenharmony_ci	struct phylink_link_state link_config;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	/* The current settings */
6762306a36Sopenharmony_ci	phy_interface_t cur_interface;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	struct gpio_desc *link_gpio;
7062306a36Sopenharmony_ci	unsigned int link_irq;
7162306a36Sopenharmony_ci	struct timer_list link_poll;
7262306a36Sopenharmony_ci	void (*get_fixed_state)(struct net_device *dev,
7362306a36Sopenharmony_ci				struct phylink_link_state *s);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	struct mutex state_mutex;
7662306a36Sopenharmony_ci	struct phylink_link_state phy_state;
7762306a36Sopenharmony_ci	struct work_struct resolve;
7862306a36Sopenharmony_ci	unsigned int pcs_neg_mode;
7962306a36Sopenharmony_ci	unsigned int pcs_state;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	bool mac_link_dropped;
8262306a36Sopenharmony_ci	bool using_mac_select_pcs;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	struct sfp_bus *sfp_bus;
8562306a36Sopenharmony_ci	bool sfp_may_have_phy;
8662306a36Sopenharmony_ci	DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
8762306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
8862306a36Sopenharmony_ci	u8 sfp_port;
8962306a36Sopenharmony_ci};
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci#define phylink_printk(level, pl, fmt, ...) \
9262306a36Sopenharmony_ci	do { \
9362306a36Sopenharmony_ci		if ((pl)->config->type == PHYLINK_NETDEV) \
9462306a36Sopenharmony_ci			netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
9562306a36Sopenharmony_ci		else if ((pl)->config->type == PHYLINK_DEV) \
9662306a36Sopenharmony_ci			dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
9762306a36Sopenharmony_ci	} while (0)
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci#define phylink_err(pl, fmt, ...) \
10062306a36Sopenharmony_ci	phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
10162306a36Sopenharmony_ci#define phylink_warn(pl, fmt, ...) \
10262306a36Sopenharmony_ci	phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
10362306a36Sopenharmony_ci#define phylink_info(pl, fmt, ...) \
10462306a36Sopenharmony_ci	phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
10562306a36Sopenharmony_ci#if defined(CONFIG_DYNAMIC_DEBUG)
10662306a36Sopenharmony_ci#define phylink_dbg(pl, fmt, ...) \
10762306a36Sopenharmony_cido {									\
10862306a36Sopenharmony_ci	if ((pl)->config->type == PHYLINK_NETDEV)			\
10962306a36Sopenharmony_ci		netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__);		\
11062306a36Sopenharmony_ci	else if ((pl)->config->type == PHYLINK_DEV)			\
11162306a36Sopenharmony_ci		dev_dbg((pl)->dev, fmt, ##__VA_ARGS__);			\
11262306a36Sopenharmony_ci} while (0)
11362306a36Sopenharmony_ci#elif defined(DEBUG)
11462306a36Sopenharmony_ci#define phylink_dbg(pl, fmt, ...)					\
11562306a36Sopenharmony_ci	phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
11662306a36Sopenharmony_ci#else
11762306a36Sopenharmony_ci#define phylink_dbg(pl, fmt, ...)					\
11862306a36Sopenharmony_ci({									\
11962306a36Sopenharmony_ci	if (0)								\
12062306a36Sopenharmony_ci		phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__);	\
12162306a36Sopenharmony_ci})
12262306a36Sopenharmony_ci#endif
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci/**
12562306a36Sopenharmony_ci * phylink_set_port_modes() - set the port type modes in the ethtool mask
12662306a36Sopenharmony_ci * @mask: ethtool link mode mask
12762306a36Sopenharmony_ci *
12862306a36Sopenharmony_ci * Sets all the port type modes in the ethtool mask.  MAC drivers should
12962306a36Sopenharmony_ci * use this in their 'validate' callback.
13062306a36Sopenharmony_ci */
13162306a36Sopenharmony_civoid phylink_set_port_modes(unsigned long *mask)
13262306a36Sopenharmony_ci{
13362306a36Sopenharmony_ci	phylink_set(mask, TP);
13462306a36Sopenharmony_ci	phylink_set(mask, AUI);
13562306a36Sopenharmony_ci	phylink_set(mask, MII);
13662306a36Sopenharmony_ci	phylink_set(mask, FIBRE);
13762306a36Sopenharmony_ci	phylink_set(mask, BNC);
13862306a36Sopenharmony_ci	phylink_set(mask, Backplane);
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_set_port_modes);
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic int phylink_is_empty_linkmode(const unsigned long *linkmode)
14362306a36Sopenharmony_ci{
14462306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	phylink_set_port_modes(tmp);
14762306a36Sopenharmony_ci	phylink_set(tmp, Autoneg);
14862306a36Sopenharmony_ci	phylink_set(tmp, Pause);
14962306a36Sopenharmony_ci	phylink_set(tmp, Asym_Pause);
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	return linkmode_subset(linkmode, tmp);
15262306a36Sopenharmony_ci}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cistatic const char *phylink_an_mode_str(unsigned int mode)
15562306a36Sopenharmony_ci{
15662306a36Sopenharmony_ci	static const char *modestr[] = {
15762306a36Sopenharmony_ci		[MLO_AN_PHY] = "phy",
15862306a36Sopenharmony_ci		[MLO_AN_FIXED] = "fixed",
15962306a36Sopenharmony_ci		[MLO_AN_INBAND] = "inband",
16062306a36Sopenharmony_ci	};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
16362306a36Sopenharmony_ci}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_cistatic unsigned int phylink_interface_signal_rate(phy_interface_t interface)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	switch (interface) {
16862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_SGMII:
16962306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_1000BASEX: /* 1.25Mbd */
17062306a36Sopenharmony_ci		return 1250;
17162306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_2500BASEX: /* 3.125Mbd */
17262306a36Sopenharmony_ci		return 3125;
17362306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_5GBASER: /* 5.15625Mbd */
17462306a36Sopenharmony_ci		return 5156;
17562306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_10GBASER: /* 10.3125Mbd */
17662306a36Sopenharmony_ci		return 10313;
17762306a36Sopenharmony_ci	default:
17862306a36Sopenharmony_ci		return 0;
17962306a36Sopenharmony_ci	}
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci/**
18362306a36Sopenharmony_ci * phylink_interface_max_speed() - get the maximum speed of a phy interface
18462306a36Sopenharmony_ci * @interface: phy interface mode defined by &typedef phy_interface_t
18562306a36Sopenharmony_ci *
18662306a36Sopenharmony_ci * Determine the maximum speed of a phy interface. This is intended to help
18762306a36Sopenharmony_ci * determine the correct speed to pass to the MAC when the phy is performing
18862306a36Sopenharmony_ci * rate matching.
18962306a36Sopenharmony_ci *
19062306a36Sopenharmony_ci * Return: The maximum speed of @interface
19162306a36Sopenharmony_ci */
19262306a36Sopenharmony_cistatic int phylink_interface_max_speed(phy_interface_t interface)
19362306a36Sopenharmony_ci{
19462306a36Sopenharmony_ci	switch (interface) {
19562306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_100BASEX:
19662306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_REVRMII:
19762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RMII:
19862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_SMII:
19962306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_REVMII:
20062306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_MII:
20162306a36Sopenharmony_ci		return SPEED_100;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_TBI:
20462306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_MOCA:
20562306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RTBI:
20662306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_1000BASEX:
20762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_1000BASEKX:
20862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_TRGMII:
20962306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RGMII_TXID:
21062306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RGMII_RXID:
21162306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RGMII_ID:
21262306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RGMII:
21362306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_PSGMII:
21462306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_QSGMII:
21562306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_QUSGMII:
21662306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_SGMII:
21762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_GMII:
21862306a36Sopenharmony_ci		return SPEED_1000;
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_2500BASEX:
22162306a36Sopenharmony_ci		return SPEED_2500;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_5GBASER:
22462306a36Sopenharmony_ci		return SPEED_5000;
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_XGMII:
22762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RXAUI:
22862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_XAUI:
22962306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_10GBASER:
23062306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_10GKR:
23162306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_USXGMII:
23262306a36Sopenharmony_ci		return SPEED_10000;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_25GBASER:
23562306a36Sopenharmony_ci		return SPEED_25000;
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_XLGMII:
23862306a36Sopenharmony_ci		return SPEED_40000;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_INTERNAL:
24162306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_NA:
24262306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_MAX:
24362306a36Sopenharmony_ci		/* No idea! Garbage in, unknown out */
24462306a36Sopenharmony_ci		return SPEED_UNKNOWN;
24562306a36Sopenharmony_ci	}
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	/* If we get here, someone forgot to add an interface mode above */
24862306a36Sopenharmony_ci	WARN_ON_ONCE(1);
24962306a36Sopenharmony_ci	return SPEED_UNKNOWN;
25062306a36Sopenharmony_ci}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci/**
25362306a36Sopenharmony_ci * phylink_caps_to_linkmodes() - Convert capabilities to ethtool link modes
25462306a36Sopenharmony_ci * @linkmodes: ethtool linkmode mask (must be already initialised)
25562306a36Sopenharmony_ci * @caps: bitmask of MAC capabilities
25662306a36Sopenharmony_ci *
25762306a36Sopenharmony_ci * Set all possible pause, speed and duplex linkmodes in @linkmodes that are
25862306a36Sopenharmony_ci * supported by the @caps. @linkmodes must have been initialised previously.
25962306a36Sopenharmony_ci */
26062306a36Sopenharmony_civoid phylink_caps_to_linkmodes(unsigned long *linkmodes, unsigned long caps)
26162306a36Sopenharmony_ci{
26262306a36Sopenharmony_ci	if (caps & MAC_SYM_PAUSE)
26362306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_Pause_BIT, linkmodes);
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	if (caps & MAC_ASYM_PAUSE)
26662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, linkmodes);
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	if (caps & MAC_10HD) {
26962306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, linkmodes);
27062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10baseT1S_Half_BIT, linkmodes);
27162306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT, linkmodes);
27262306a36Sopenharmony_ci	}
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci	if (caps & MAC_10FD) {
27562306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, linkmodes);
27662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, linkmodes);
27762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10baseT1S_Full_BIT, linkmodes);
27862306a36Sopenharmony_ci	}
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	if (caps & MAC_100HD) {
28162306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, linkmodes);
28262306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100baseFX_Half_BIT, linkmodes);
28362306a36Sopenharmony_ci	}
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci	if (caps & MAC_100FD) {
28662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, linkmodes);
28762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT, linkmodes);
28862306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100baseFX_Full_BIT, linkmodes);
28962306a36Sopenharmony_ci	}
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	if (caps & MAC_1000HD)
29262306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, linkmodes);
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci	if (caps & MAC_1000FD) {
29562306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, linkmodes);
29662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, linkmodes);
29762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, linkmodes);
29862306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT, linkmodes);
29962306a36Sopenharmony_ci	}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	if (caps & MAC_2500FD) {
30262306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, linkmodes);
30362306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, linkmodes);
30462306a36Sopenharmony_ci	}
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	if (caps & MAC_5000FD)
30762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, linkmodes);
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci	if (caps & MAC_10000FD) {
31062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, linkmodes);
31162306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, linkmodes);
31262306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, linkmodes);
31362306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT, linkmodes);
31462306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10000baseCR_Full_BIT, linkmodes);
31562306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, linkmodes);
31662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, linkmodes);
31762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT, linkmodes);
31862306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, linkmodes);
31962306a36Sopenharmony_ci	}
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci	if (caps & MAC_25000FD) {
32262306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, linkmodes);
32362306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, linkmodes);
32462306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, linkmodes);
32562306a36Sopenharmony_ci	}
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	if (caps & MAC_40000FD) {
32862306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, linkmodes);
32962306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, linkmodes);
33062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, linkmodes);
33162306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, linkmodes);
33262306a36Sopenharmony_ci	}
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	if (caps & MAC_50000FD) {
33562306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT, linkmodes);
33662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT, linkmodes);
33762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT, linkmodes);
33862306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_50000baseKR_Full_BIT, linkmodes);
33962306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_50000baseSR_Full_BIT, linkmodes);
34062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_50000baseCR_Full_BIT, linkmodes);
34162306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
34262306a36Sopenharmony_ci			  linkmodes);
34362306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_50000baseDR_Full_BIT, linkmodes);
34462306a36Sopenharmony_ci	}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	if (caps & MAC_56000FD) {
34762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT, linkmodes);
34862306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT, linkmodes);
34962306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT, linkmodes);
35062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT, linkmodes);
35162306a36Sopenharmony_ci	}
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	if (caps & MAC_100000FD) {
35462306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, linkmodes);
35562306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT, linkmodes);
35662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, linkmodes);
35762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
35862306a36Sopenharmony_ci			  linkmodes);
35962306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT, linkmodes);
36062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT, linkmodes);
36162306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT, linkmodes);
36262306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT,
36362306a36Sopenharmony_ci			  linkmodes);
36462306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT, linkmodes);
36562306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseKR_Full_BIT, linkmodes);
36662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseSR_Full_BIT, linkmodes);
36762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseLR_ER_FR_Full_BIT,
36862306a36Sopenharmony_ci			  linkmodes);
36962306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseCR_Full_BIT, linkmodes);
37062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_100000baseDR_Full_BIT, linkmodes);
37162306a36Sopenharmony_ci	}
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	if (caps & MAC_200000FD) {
37462306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT, linkmodes);
37562306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT, linkmodes);
37662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
37762306a36Sopenharmony_ci			  linkmodes);
37862306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT, linkmodes);
37962306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT, linkmodes);
38062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseKR2_Full_BIT, linkmodes);
38162306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseSR2_Full_BIT, linkmodes);
38262306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseLR2_ER2_FR2_Full_BIT,
38362306a36Sopenharmony_ci			  linkmodes);
38462306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseDR2_Full_BIT, linkmodes);
38562306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_200000baseCR2_Full_BIT, linkmodes);
38662306a36Sopenharmony_ci	}
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	if (caps & MAC_400000FD) {
38962306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseKR8_Full_BIT, linkmodes);
39062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseSR8_Full_BIT, linkmodes);
39162306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseLR8_ER8_FR8_Full_BIT,
39262306a36Sopenharmony_ci			  linkmodes);
39362306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseDR8_Full_BIT, linkmodes);
39462306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseCR8_Full_BIT, linkmodes);
39562306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseKR4_Full_BIT, linkmodes);
39662306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseSR4_Full_BIT, linkmodes);
39762306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseLR4_ER4_FR4_Full_BIT,
39862306a36Sopenharmony_ci			  linkmodes);
39962306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseDR4_Full_BIT, linkmodes);
40062306a36Sopenharmony_ci		__set_bit(ETHTOOL_LINK_MODE_400000baseCR4_Full_BIT, linkmodes);
40162306a36Sopenharmony_ci	}
40262306a36Sopenharmony_ci}
40362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_caps_to_linkmodes);
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_cistatic struct {
40662306a36Sopenharmony_ci	unsigned long mask;
40762306a36Sopenharmony_ci	int speed;
40862306a36Sopenharmony_ci	unsigned int duplex;
40962306a36Sopenharmony_ci} phylink_caps_params[] = {
41062306a36Sopenharmony_ci	{ MAC_400000FD, SPEED_400000, DUPLEX_FULL },
41162306a36Sopenharmony_ci	{ MAC_200000FD, SPEED_200000, DUPLEX_FULL },
41262306a36Sopenharmony_ci	{ MAC_100000FD, SPEED_100000, DUPLEX_FULL },
41362306a36Sopenharmony_ci	{ MAC_56000FD,  SPEED_56000,  DUPLEX_FULL },
41462306a36Sopenharmony_ci	{ MAC_50000FD,  SPEED_50000,  DUPLEX_FULL },
41562306a36Sopenharmony_ci	{ MAC_40000FD,  SPEED_40000,  DUPLEX_FULL },
41662306a36Sopenharmony_ci	{ MAC_25000FD,  SPEED_25000,  DUPLEX_FULL },
41762306a36Sopenharmony_ci	{ MAC_20000FD,  SPEED_20000,  DUPLEX_FULL },
41862306a36Sopenharmony_ci	{ MAC_10000FD,  SPEED_10000,  DUPLEX_FULL },
41962306a36Sopenharmony_ci	{ MAC_5000FD,   SPEED_5000,   DUPLEX_FULL },
42062306a36Sopenharmony_ci	{ MAC_2500FD,   SPEED_2500,   DUPLEX_FULL },
42162306a36Sopenharmony_ci	{ MAC_1000FD,   SPEED_1000,   DUPLEX_FULL },
42262306a36Sopenharmony_ci	{ MAC_1000HD,   SPEED_1000,   DUPLEX_HALF },
42362306a36Sopenharmony_ci	{ MAC_100FD,    SPEED_100,    DUPLEX_FULL },
42462306a36Sopenharmony_ci	{ MAC_100HD,    SPEED_100,    DUPLEX_HALF },
42562306a36Sopenharmony_ci	{ MAC_10FD,     SPEED_10,     DUPLEX_FULL },
42662306a36Sopenharmony_ci	{ MAC_10HD,     SPEED_10,     DUPLEX_HALF },
42762306a36Sopenharmony_ci};
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci/**
43062306a36Sopenharmony_ci * phylink_limit_mac_speed - limit the phylink_config to a maximum speed
43162306a36Sopenharmony_ci * @config: pointer to a &struct phylink_config
43262306a36Sopenharmony_ci * @max_speed: maximum speed
43362306a36Sopenharmony_ci *
43462306a36Sopenharmony_ci * Mask off MAC capabilities for speeds higher than the @max_speed parameter.
43562306a36Sopenharmony_ci * Any further motifications of config.mac_capabilities will override this.
43662306a36Sopenharmony_ci */
43762306a36Sopenharmony_civoid phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed)
43862306a36Sopenharmony_ci{
43962306a36Sopenharmony_ci	int i;
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(phylink_caps_params) &&
44262306a36Sopenharmony_ci		    phylink_caps_params[i].speed > max_speed; i++)
44362306a36Sopenharmony_ci		config->mac_capabilities &= ~phylink_caps_params[i].mask;
44462306a36Sopenharmony_ci}
44562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_limit_mac_speed);
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci/**
44862306a36Sopenharmony_ci * phylink_cap_from_speed_duplex - Get mac capability from speed/duplex
44962306a36Sopenharmony_ci * @speed: the speed to search for
45062306a36Sopenharmony_ci * @duplex: the duplex to search for
45162306a36Sopenharmony_ci *
45262306a36Sopenharmony_ci * Find the mac capability for a given speed and duplex.
45362306a36Sopenharmony_ci *
45462306a36Sopenharmony_ci * Return: A mask with the mac capability patching @speed and @duplex, or 0 if
45562306a36Sopenharmony_ci *         there were no matches.
45662306a36Sopenharmony_ci */
45762306a36Sopenharmony_cistatic unsigned long phylink_cap_from_speed_duplex(int speed,
45862306a36Sopenharmony_ci						   unsigned int duplex)
45962306a36Sopenharmony_ci{
46062306a36Sopenharmony_ci	int i;
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(phylink_caps_params); i++) {
46362306a36Sopenharmony_ci		if (speed == phylink_caps_params[i].speed &&
46462306a36Sopenharmony_ci		    duplex == phylink_caps_params[i].duplex)
46562306a36Sopenharmony_ci			return phylink_caps_params[i].mask;
46662306a36Sopenharmony_ci	}
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci	return 0;
46962306a36Sopenharmony_ci}
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci/**
47262306a36Sopenharmony_ci * phylink_get_capabilities() - get capabilities for a given MAC
47362306a36Sopenharmony_ci * @interface: phy interface mode defined by &typedef phy_interface_t
47462306a36Sopenharmony_ci * @mac_capabilities: bitmask of MAC capabilities
47562306a36Sopenharmony_ci * @rate_matching: type of rate matching being performed
47662306a36Sopenharmony_ci *
47762306a36Sopenharmony_ci * Get the MAC capabilities that are supported by the @interface mode and
47862306a36Sopenharmony_ci * @mac_capabilities.
47962306a36Sopenharmony_ci */
48062306a36Sopenharmony_ciunsigned long phylink_get_capabilities(phy_interface_t interface,
48162306a36Sopenharmony_ci				       unsigned long mac_capabilities,
48262306a36Sopenharmony_ci				       int rate_matching)
48362306a36Sopenharmony_ci{
48462306a36Sopenharmony_ci	int max_speed = phylink_interface_max_speed(interface);
48562306a36Sopenharmony_ci	unsigned long caps = MAC_SYM_PAUSE | MAC_ASYM_PAUSE;
48662306a36Sopenharmony_ci	unsigned long matched_caps = 0;
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	switch (interface) {
48962306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_USXGMII:
49062306a36Sopenharmony_ci		caps |= MAC_10000FD | MAC_5000FD | MAC_2500FD;
49162306a36Sopenharmony_ci		fallthrough;
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RGMII_TXID:
49462306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RGMII_RXID:
49562306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RGMII_ID:
49662306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RGMII:
49762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_PSGMII:
49862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_QSGMII:
49962306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_QUSGMII:
50062306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_SGMII:
50162306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_GMII:
50262306a36Sopenharmony_ci		caps |= MAC_1000HD | MAC_1000FD;
50362306a36Sopenharmony_ci		fallthrough;
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_REVRMII:
50662306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RMII:
50762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_SMII:
50862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_REVMII:
50962306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_MII:
51062306a36Sopenharmony_ci		caps |= MAC_10HD | MAC_10FD;
51162306a36Sopenharmony_ci		fallthrough;
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_100BASEX:
51462306a36Sopenharmony_ci		caps |= MAC_100HD | MAC_100FD;
51562306a36Sopenharmony_ci		break;
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_TBI:
51862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_MOCA:
51962306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RTBI:
52062306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_1000BASEX:
52162306a36Sopenharmony_ci		caps |= MAC_1000HD;
52262306a36Sopenharmony_ci		fallthrough;
52362306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_1000BASEKX:
52462306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_TRGMII:
52562306a36Sopenharmony_ci		caps |= MAC_1000FD;
52662306a36Sopenharmony_ci		break;
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_2500BASEX:
52962306a36Sopenharmony_ci		caps |= MAC_2500FD;
53062306a36Sopenharmony_ci		break;
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_5GBASER:
53362306a36Sopenharmony_ci		caps |= MAC_5000FD;
53462306a36Sopenharmony_ci		break;
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_XGMII:
53762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_RXAUI:
53862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_XAUI:
53962306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_10GBASER:
54062306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_10GKR:
54162306a36Sopenharmony_ci		caps |= MAC_10000FD;
54262306a36Sopenharmony_ci		break;
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_25GBASER:
54562306a36Sopenharmony_ci		caps |= MAC_25000FD;
54662306a36Sopenharmony_ci		break;
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_XLGMII:
54962306a36Sopenharmony_ci		caps |= MAC_40000FD;
55062306a36Sopenharmony_ci		break;
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_INTERNAL:
55362306a36Sopenharmony_ci		caps |= ~0;
55462306a36Sopenharmony_ci		break;
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_NA:
55762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_MAX:
55862306a36Sopenharmony_ci		break;
55962306a36Sopenharmony_ci	}
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci	switch (rate_matching) {
56262306a36Sopenharmony_ci	case RATE_MATCH_OPEN_LOOP:
56362306a36Sopenharmony_ci		/* TODO */
56462306a36Sopenharmony_ci		fallthrough;
56562306a36Sopenharmony_ci	case RATE_MATCH_NONE:
56662306a36Sopenharmony_ci		matched_caps = 0;
56762306a36Sopenharmony_ci		break;
56862306a36Sopenharmony_ci	case RATE_MATCH_PAUSE: {
56962306a36Sopenharmony_ci		/* The MAC must support asymmetric pause towards the local
57062306a36Sopenharmony_ci		 * device for this. We could allow just symmetric pause, but
57162306a36Sopenharmony_ci		 * then we might have to renegotiate if the link partner
57262306a36Sopenharmony_ci		 * doesn't support pause. This is because there's no way to
57362306a36Sopenharmony_ci		 * accept pause frames without transmitting them if we only
57462306a36Sopenharmony_ci		 * support symmetric pause.
57562306a36Sopenharmony_ci		 */
57662306a36Sopenharmony_ci		if (!(mac_capabilities & MAC_SYM_PAUSE) ||
57762306a36Sopenharmony_ci		    !(mac_capabilities & MAC_ASYM_PAUSE))
57862306a36Sopenharmony_ci			break;
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci		/* We can't adapt if the MAC doesn't support the interface's
58162306a36Sopenharmony_ci		 * max speed at full duplex.
58262306a36Sopenharmony_ci		 */
58362306a36Sopenharmony_ci		if (mac_capabilities &
58462306a36Sopenharmony_ci		    phylink_cap_from_speed_duplex(max_speed, DUPLEX_FULL)) {
58562306a36Sopenharmony_ci			/* Although a duplex-matching phy might exist, we
58662306a36Sopenharmony_ci			 * conservatively remove these modes because the MAC
58762306a36Sopenharmony_ci			 * will not be aware of the half-duplex nature of the
58862306a36Sopenharmony_ci			 * link.
58962306a36Sopenharmony_ci			 */
59062306a36Sopenharmony_ci			matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
59162306a36Sopenharmony_ci			matched_caps &= ~(MAC_1000HD | MAC_100HD | MAC_10HD);
59262306a36Sopenharmony_ci		}
59362306a36Sopenharmony_ci		break;
59462306a36Sopenharmony_ci	}
59562306a36Sopenharmony_ci	case RATE_MATCH_CRS:
59662306a36Sopenharmony_ci		/* The MAC must support half duplex at the interface's max
59762306a36Sopenharmony_ci		 * speed.
59862306a36Sopenharmony_ci		 */
59962306a36Sopenharmony_ci		if (mac_capabilities &
60062306a36Sopenharmony_ci		    phylink_cap_from_speed_duplex(max_speed, DUPLEX_HALF)) {
60162306a36Sopenharmony_ci			matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
60262306a36Sopenharmony_ci			matched_caps &= mac_capabilities;
60362306a36Sopenharmony_ci		}
60462306a36Sopenharmony_ci		break;
60562306a36Sopenharmony_ci	}
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ci	return (caps & mac_capabilities) | matched_caps;
60862306a36Sopenharmony_ci}
60962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_get_capabilities);
61062306a36Sopenharmony_ci
61162306a36Sopenharmony_ci/**
61262306a36Sopenharmony_ci * phylink_validate_mask_caps() - Restrict link modes based on caps
61362306a36Sopenharmony_ci * @supported: ethtool bitmask for supported link modes.
61462306a36Sopenharmony_ci * @state: pointer to a &struct phylink_link_state.
61562306a36Sopenharmony_ci * @mac_capabilities: bitmask of MAC capabilities
61662306a36Sopenharmony_ci *
61762306a36Sopenharmony_ci * Calculate the supported link modes based on @mac_capabilities, and restrict
61862306a36Sopenharmony_ci * @supported and @state based on that. Use this function if your capabiliies
61962306a36Sopenharmony_ci * aren't constant, such as if they vary depending on the interface.
62062306a36Sopenharmony_ci */
62162306a36Sopenharmony_civoid phylink_validate_mask_caps(unsigned long *supported,
62262306a36Sopenharmony_ci				struct phylink_link_state *state,
62362306a36Sopenharmony_ci				unsigned long mac_capabilities)
62462306a36Sopenharmony_ci{
62562306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
62662306a36Sopenharmony_ci	unsigned long caps;
62762306a36Sopenharmony_ci
62862306a36Sopenharmony_ci	phylink_set_port_modes(mask);
62962306a36Sopenharmony_ci	phylink_set(mask, Autoneg);
63062306a36Sopenharmony_ci	caps = phylink_get_capabilities(state->interface, mac_capabilities,
63162306a36Sopenharmony_ci					state->rate_matching);
63262306a36Sopenharmony_ci	phylink_caps_to_linkmodes(mask, caps);
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci	linkmode_and(supported, supported, mask);
63562306a36Sopenharmony_ci	linkmode_and(state->advertising, state->advertising, mask);
63662306a36Sopenharmony_ci}
63762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_validate_mask_caps);
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci/**
64062306a36Sopenharmony_ci * phylink_generic_validate() - generic validate() callback implementation
64162306a36Sopenharmony_ci * @config: a pointer to a &struct phylink_config.
64262306a36Sopenharmony_ci * @supported: ethtool bitmask for supported link modes.
64362306a36Sopenharmony_ci * @state: a pointer to a &struct phylink_link_state.
64462306a36Sopenharmony_ci *
64562306a36Sopenharmony_ci * Generic implementation of the validate() callback that MAC drivers can
64662306a36Sopenharmony_ci * use when they pass the range of supported interfaces and MAC capabilities.
64762306a36Sopenharmony_ci */
64862306a36Sopenharmony_civoid phylink_generic_validate(struct phylink_config *config,
64962306a36Sopenharmony_ci			      unsigned long *supported,
65062306a36Sopenharmony_ci			      struct phylink_link_state *state)
65162306a36Sopenharmony_ci{
65262306a36Sopenharmony_ci	phylink_validate_mask_caps(supported, state, config->mac_capabilities);
65362306a36Sopenharmony_ci}
65462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_generic_validate);
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_cistatic int phylink_validate_mac_and_pcs(struct phylink *pl,
65762306a36Sopenharmony_ci					unsigned long *supported,
65862306a36Sopenharmony_ci					struct phylink_link_state *state)
65962306a36Sopenharmony_ci{
66062306a36Sopenharmony_ci	struct phylink_pcs *pcs;
66162306a36Sopenharmony_ci	int ret;
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci	/* Get the PCS for this interface mode */
66462306a36Sopenharmony_ci	if (pl->using_mac_select_pcs) {
66562306a36Sopenharmony_ci		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
66662306a36Sopenharmony_ci		if (IS_ERR(pcs))
66762306a36Sopenharmony_ci			return PTR_ERR(pcs);
66862306a36Sopenharmony_ci	} else {
66962306a36Sopenharmony_ci		pcs = pl->pcs;
67062306a36Sopenharmony_ci	}
67162306a36Sopenharmony_ci
67262306a36Sopenharmony_ci	if (pcs) {
67362306a36Sopenharmony_ci		/* The PCS, if present, must be setup before phylink_create()
67462306a36Sopenharmony_ci		 * has been called. If the ops is not initialised, print an
67562306a36Sopenharmony_ci		 * error and backtrace rather than oopsing the kernel.
67662306a36Sopenharmony_ci		 */
67762306a36Sopenharmony_ci		if (!pcs->ops) {
67862306a36Sopenharmony_ci			phylink_err(pl, "interface %s: uninitialised PCS\n",
67962306a36Sopenharmony_ci				    phy_modes(state->interface));
68062306a36Sopenharmony_ci			dump_stack();
68162306a36Sopenharmony_ci			return -EINVAL;
68262306a36Sopenharmony_ci		}
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci		/* Validate the link parameters with the PCS */
68562306a36Sopenharmony_ci		if (pcs->ops->pcs_validate) {
68662306a36Sopenharmony_ci			ret = pcs->ops->pcs_validate(pcs, supported, state);
68762306a36Sopenharmony_ci			if (ret < 0 || phylink_is_empty_linkmode(supported))
68862306a36Sopenharmony_ci				return -EINVAL;
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_ci			/* Ensure the advertising mask is a subset of the
69162306a36Sopenharmony_ci			 * supported mask.
69262306a36Sopenharmony_ci			 */
69362306a36Sopenharmony_ci			linkmode_and(state->advertising, state->advertising,
69462306a36Sopenharmony_ci				     supported);
69562306a36Sopenharmony_ci		}
69662306a36Sopenharmony_ci	}
69762306a36Sopenharmony_ci
69862306a36Sopenharmony_ci	/* Then validate the link parameters with the MAC */
69962306a36Sopenharmony_ci	if (pl->mac_ops->validate)
70062306a36Sopenharmony_ci		pl->mac_ops->validate(pl->config, supported, state);
70162306a36Sopenharmony_ci	else
70262306a36Sopenharmony_ci		phylink_generic_validate(pl->config, supported, state);
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_ci	return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
70562306a36Sopenharmony_ci}
70662306a36Sopenharmony_ci
70762306a36Sopenharmony_cistatic int phylink_validate_mask(struct phylink *pl, unsigned long *supported,
70862306a36Sopenharmony_ci				 struct phylink_link_state *state,
70962306a36Sopenharmony_ci				 const unsigned long *interfaces)
71062306a36Sopenharmony_ci{
71162306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
71262306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
71362306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(s);
71462306a36Sopenharmony_ci	struct phylink_link_state t;
71562306a36Sopenharmony_ci	int intf;
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ci	for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) {
71862306a36Sopenharmony_ci		if (test_bit(intf, interfaces)) {
71962306a36Sopenharmony_ci			linkmode_copy(s, supported);
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci			t = *state;
72262306a36Sopenharmony_ci			t.interface = intf;
72362306a36Sopenharmony_ci			if (!phylink_validate_mac_and_pcs(pl, s, &t)) {
72462306a36Sopenharmony_ci				linkmode_or(all_s, all_s, s);
72562306a36Sopenharmony_ci				linkmode_or(all_adv, all_adv, t.advertising);
72662306a36Sopenharmony_ci			}
72762306a36Sopenharmony_ci		}
72862306a36Sopenharmony_ci	}
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ci	linkmode_copy(supported, all_s);
73162306a36Sopenharmony_ci	linkmode_copy(state->advertising, all_adv);
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci	return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
73462306a36Sopenharmony_ci}
73562306a36Sopenharmony_ci
73662306a36Sopenharmony_cistatic int phylink_validate(struct phylink *pl, unsigned long *supported,
73762306a36Sopenharmony_ci			    struct phylink_link_state *state)
73862306a36Sopenharmony_ci{
73962306a36Sopenharmony_ci	const unsigned long *interfaces = pl->config->supported_interfaces;
74062306a36Sopenharmony_ci
74162306a36Sopenharmony_ci	if (state->interface == PHY_INTERFACE_MODE_NA)
74262306a36Sopenharmony_ci		return phylink_validate_mask(pl, supported, state, interfaces);
74362306a36Sopenharmony_ci
74462306a36Sopenharmony_ci	if (!test_bit(state->interface, interfaces))
74562306a36Sopenharmony_ci		return -EINVAL;
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ci	return phylink_validate_mac_and_pcs(pl, supported, state);
74862306a36Sopenharmony_ci}
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_cistatic int phylink_parse_fixedlink(struct phylink *pl,
75162306a36Sopenharmony_ci				   const struct fwnode_handle *fwnode)
75262306a36Sopenharmony_ci{
75362306a36Sopenharmony_ci	struct fwnode_handle *fixed_node;
75462306a36Sopenharmony_ci	bool pause, asym_pause, autoneg;
75562306a36Sopenharmony_ci	const struct phy_setting *s;
75662306a36Sopenharmony_ci	struct gpio_desc *desc;
75762306a36Sopenharmony_ci	u32 speed;
75862306a36Sopenharmony_ci	int ret;
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_ci	fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
76162306a36Sopenharmony_ci	if (fixed_node) {
76262306a36Sopenharmony_ci		ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci		pl->link_config.speed = speed;
76562306a36Sopenharmony_ci		pl->link_config.duplex = DUPLEX_HALF;
76662306a36Sopenharmony_ci
76762306a36Sopenharmony_ci		if (fwnode_property_read_bool(fixed_node, "full-duplex"))
76862306a36Sopenharmony_ci			pl->link_config.duplex = DUPLEX_FULL;
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci		/* We treat the "pause" and "asym-pause" terminology as
77162306a36Sopenharmony_ci		 * defining the link partner's ability.
77262306a36Sopenharmony_ci		 */
77362306a36Sopenharmony_ci		if (fwnode_property_read_bool(fixed_node, "pause"))
77462306a36Sopenharmony_ci			__set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
77562306a36Sopenharmony_ci				  pl->link_config.lp_advertising);
77662306a36Sopenharmony_ci		if (fwnode_property_read_bool(fixed_node, "asym-pause"))
77762306a36Sopenharmony_ci			__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
77862306a36Sopenharmony_ci				  pl->link_config.lp_advertising);
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci		if (ret == 0) {
78162306a36Sopenharmony_ci			desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
78262306a36Sopenharmony_ci						      GPIOD_IN, "?");
78362306a36Sopenharmony_ci
78462306a36Sopenharmony_ci			if (!IS_ERR(desc))
78562306a36Sopenharmony_ci				pl->link_gpio = desc;
78662306a36Sopenharmony_ci			else if (desc == ERR_PTR(-EPROBE_DEFER))
78762306a36Sopenharmony_ci				ret = -EPROBE_DEFER;
78862306a36Sopenharmony_ci		}
78962306a36Sopenharmony_ci		fwnode_handle_put(fixed_node);
79062306a36Sopenharmony_ci
79162306a36Sopenharmony_ci		if (ret)
79262306a36Sopenharmony_ci			return ret;
79362306a36Sopenharmony_ci	} else {
79462306a36Sopenharmony_ci		u32 prop[5];
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_ci		ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
79762306a36Sopenharmony_ci						     NULL, 0);
79862306a36Sopenharmony_ci		if (ret != ARRAY_SIZE(prop)) {
79962306a36Sopenharmony_ci			phylink_err(pl, "broken fixed-link?\n");
80062306a36Sopenharmony_ci			return -EINVAL;
80162306a36Sopenharmony_ci		}
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci		ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
80462306a36Sopenharmony_ci						     prop, ARRAY_SIZE(prop));
80562306a36Sopenharmony_ci		if (!ret) {
80662306a36Sopenharmony_ci			pl->link_config.duplex = prop[1] ?
80762306a36Sopenharmony_ci						DUPLEX_FULL : DUPLEX_HALF;
80862306a36Sopenharmony_ci			pl->link_config.speed = prop[2];
80962306a36Sopenharmony_ci			if (prop[3])
81062306a36Sopenharmony_ci				__set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
81162306a36Sopenharmony_ci					  pl->link_config.lp_advertising);
81262306a36Sopenharmony_ci			if (prop[4])
81362306a36Sopenharmony_ci				__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
81462306a36Sopenharmony_ci					  pl->link_config.lp_advertising);
81562306a36Sopenharmony_ci		}
81662306a36Sopenharmony_ci	}
81762306a36Sopenharmony_ci
81862306a36Sopenharmony_ci	if (pl->link_config.speed > SPEED_1000 &&
81962306a36Sopenharmony_ci	    pl->link_config.duplex != DUPLEX_FULL)
82062306a36Sopenharmony_ci		phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
82162306a36Sopenharmony_ci			     pl->link_config.speed);
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci	bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
82462306a36Sopenharmony_ci	linkmode_copy(pl->link_config.advertising, pl->supported);
82562306a36Sopenharmony_ci	phylink_validate(pl, pl->supported, &pl->link_config);
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci	pause = phylink_test(pl->supported, Pause);
82862306a36Sopenharmony_ci	asym_pause = phylink_test(pl->supported, Asym_Pause);
82962306a36Sopenharmony_ci	autoneg = phylink_test(pl->supported, Autoneg);
83062306a36Sopenharmony_ci	s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
83162306a36Sopenharmony_ci			       pl->supported, true);
83262306a36Sopenharmony_ci	linkmode_zero(pl->supported);
83362306a36Sopenharmony_ci	phylink_set(pl->supported, MII);
83462306a36Sopenharmony_ci
83562306a36Sopenharmony_ci	if (pause)
83662306a36Sopenharmony_ci		phylink_set(pl->supported, Pause);
83762306a36Sopenharmony_ci
83862306a36Sopenharmony_ci	if (asym_pause)
83962306a36Sopenharmony_ci		phylink_set(pl->supported, Asym_Pause);
84062306a36Sopenharmony_ci
84162306a36Sopenharmony_ci	if (autoneg)
84262306a36Sopenharmony_ci		phylink_set(pl->supported, Autoneg);
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_ci	if (s) {
84562306a36Sopenharmony_ci		__set_bit(s->bit, pl->supported);
84662306a36Sopenharmony_ci		__set_bit(s->bit, pl->link_config.lp_advertising);
84762306a36Sopenharmony_ci	} else {
84862306a36Sopenharmony_ci		phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
84962306a36Sopenharmony_ci			     pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
85062306a36Sopenharmony_ci			     pl->link_config.speed);
85162306a36Sopenharmony_ci	}
85262306a36Sopenharmony_ci
85362306a36Sopenharmony_ci	linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
85462306a36Sopenharmony_ci		     pl->supported);
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ci	pl->link_config.link = 1;
85762306a36Sopenharmony_ci	pl->link_config.an_complete = 1;
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_ci	return 0;
86062306a36Sopenharmony_ci}
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_cistatic int phylink_parse_mode(struct phylink *pl,
86362306a36Sopenharmony_ci			      const struct fwnode_handle *fwnode)
86462306a36Sopenharmony_ci{
86562306a36Sopenharmony_ci	struct fwnode_handle *dn;
86662306a36Sopenharmony_ci	const char *managed;
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_ci	dn = fwnode_get_named_child_node(fwnode, "fixed-link");
86962306a36Sopenharmony_ci	if (dn || fwnode_property_present(fwnode, "fixed-link"))
87062306a36Sopenharmony_ci		pl->cfg_link_an_mode = MLO_AN_FIXED;
87162306a36Sopenharmony_ci	fwnode_handle_put(dn);
87262306a36Sopenharmony_ci
87362306a36Sopenharmony_ci	if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
87462306a36Sopenharmony_ci	     strcmp(managed, "in-band-status") == 0) ||
87562306a36Sopenharmony_ci	    pl->config->ovr_an_inband) {
87662306a36Sopenharmony_ci		if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
87762306a36Sopenharmony_ci			phylink_err(pl,
87862306a36Sopenharmony_ci				    "can't use both fixed-link and in-band-status\n");
87962306a36Sopenharmony_ci			return -EINVAL;
88062306a36Sopenharmony_ci		}
88162306a36Sopenharmony_ci
88262306a36Sopenharmony_ci		linkmode_zero(pl->supported);
88362306a36Sopenharmony_ci		phylink_set(pl->supported, MII);
88462306a36Sopenharmony_ci		phylink_set(pl->supported, Autoneg);
88562306a36Sopenharmony_ci		phylink_set(pl->supported, Asym_Pause);
88662306a36Sopenharmony_ci		phylink_set(pl->supported, Pause);
88762306a36Sopenharmony_ci		pl->cfg_link_an_mode = MLO_AN_INBAND;
88862306a36Sopenharmony_ci
88962306a36Sopenharmony_ci		switch (pl->link_config.interface) {
89062306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_SGMII:
89162306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_PSGMII:
89262306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_QSGMII:
89362306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_QUSGMII:
89462306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_RGMII:
89562306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_RGMII_ID:
89662306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_RGMII_RXID:
89762306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_RGMII_TXID:
89862306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_RTBI:
89962306a36Sopenharmony_ci			phylink_set(pl->supported, 10baseT_Half);
90062306a36Sopenharmony_ci			phylink_set(pl->supported, 10baseT_Full);
90162306a36Sopenharmony_ci			phylink_set(pl->supported, 100baseT_Half);
90262306a36Sopenharmony_ci			phylink_set(pl->supported, 100baseT_Full);
90362306a36Sopenharmony_ci			phylink_set(pl->supported, 1000baseT_Half);
90462306a36Sopenharmony_ci			phylink_set(pl->supported, 1000baseT_Full);
90562306a36Sopenharmony_ci			break;
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_1000BASEX:
90862306a36Sopenharmony_ci			phylink_set(pl->supported, 1000baseX_Full);
90962306a36Sopenharmony_ci			break;
91062306a36Sopenharmony_ci
91162306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_2500BASEX:
91262306a36Sopenharmony_ci			phylink_set(pl->supported, 2500baseX_Full);
91362306a36Sopenharmony_ci			break;
91462306a36Sopenharmony_ci
91562306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_5GBASER:
91662306a36Sopenharmony_ci			phylink_set(pl->supported, 5000baseT_Full);
91762306a36Sopenharmony_ci			break;
91862306a36Sopenharmony_ci
91962306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_25GBASER:
92062306a36Sopenharmony_ci			phylink_set(pl->supported, 25000baseCR_Full);
92162306a36Sopenharmony_ci			phylink_set(pl->supported, 25000baseKR_Full);
92262306a36Sopenharmony_ci			phylink_set(pl->supported, 25000baseSR_Full);
92362306a36Sopenharmony_ci			fallthrough;
92462306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_USXGMII:
92562306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_10GKR:
92662306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_10GBASER:
92762306a36Sopenharmony_ci			phylink_set(pl->supported, 10baseT_Half);
92862306a36Sopenharmony_ci			phylink_set(pl->supported, 10baseT_Full);
92962306a36Sopenharmony_ci			phylink_set(pl->supported, 100baseT_Half);
93062306a36Sopenharmony_ci			phylink_set(pl->supported, 100baseT_Full);
93162306a36Sopenharmony_ci			phylink_set(pl->supported, 1000baseT_Half);
93262306a36Sopenharmony_ci			phylink_set(pl->supported, 1000baseT_Full);
93362306a36Sopenharmony_ci			phylink_set(pl->supported, 1000baseX_Full);
93462306a36Sopenharmony_ci			phylink_set(pl->supported, 1000baseKX_Full);
93562306a36Sopenharmony_ci			phylink_set(pl->supported, 2500baseT_Full);
93662306a36Sopenharmony_ci			phylink_set(pl->supported, 2500baseX_Full);
93762306a36Sopenharmony_ci			phylink_set(pl->supported, 5000baseT_Full);
93862306a36Sopenharmony_ci			phylink_set(pl->supported, 10000baseT_Full);
93962306a36Sopenharmony_ci			phylink_set(pl->supported, 10000baseKR_Full);
94062306a36Sopenharmony_ci			phylink_set(pl->supported, 10000baseKX4_Full);
94162306a36Sopenharmony_ci			phylink_set(pl->supported, 10000baseCR_Full);
94262306a36Sopenharmony_ci			phylink_set(pl->supported, 10000baseSR_Full);
94362306a36Sopenharmony_ci			phylink_set(pl->supported, 10000baseLR_Full);
94462306a36Sopenharmony_ci			phylink_set(pl->supported, 10000baseLRM_Full);
94562306a36Sopenharmony_ci			phylink_set(pl->supported, 10000baseER_Full);
94662306a36Sopenharmony_ci			break;
94762306a36Sopenharmony_ci
94862306a36Sopenharmony_ci		case PHY_INTERFACE_MODE_XLGMII:
94962306a36Sopenharmony_ci			phylink_set(pl->supported, 25000baseCR_Full);
95062306a36Sopenharmony_ci			phylink_set(pl->supported, 25000baseKR_Full);
95162306a36Sopenharmony_ci			phylink_set(pl->supported, 25000baseSR_Full);
95262306a36Sopenharmony_ci			phylink_set(pl->supported, 40000baseKR4_Full);
95362306a36Sopenharmony_ci			phylink_set(pl->supported, 40000baseCR4_Full);
95462306a36Sopenharmony_ci			phylink_set(pl->supported, 40000baseSR4_Full);
95562306a36Sopenharmony_ci			phylink_set(pl->supported, 40000baseLR4_Full);
95662306a36Sopenharmony_ci			phylink_set(pl->supported, 50000baseCR2_Full);
95762306a36Sopenharmony_ci			phylink_set(pl->supported, 50000baseKR2_Full);
95862306a36Sopenharmony_ci			phylink_set(pl->supported, 50000baseSR2_Full);
95962306a36Sopenharmony_ci			phylink_set(pl->supported, 50000baseKR_Full);
96062306a36Sopenharmony_ci			phylink_set(pl->supported, 50000baseSR_Full);
96162306a36Sopenharmony_ci			phylink_set(pl->supported, 50000baseCR_Full);
96262306a36Sopenharmony_ci			phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
96362306a36Sopenharmony_ci			phylink_set(pl->supported, 50000baseDR_Full);
96462306a36Sopenharmony_ci			phylink_set(pl->supported, 100000baseKR4_Full);
96562306a36Sopenharmony_ci			phylink_set(pl->supported, 100000baseSR4_Full);
96662306a36Sopenharmony_ci			phylink_set(pl->supported, 100000baseCR4_Full);
96762306a36Sopenharmony_ci			phylink_set(pl->supported, 100000baseLR4_ER4_Full);
96862306a36Sopenharmony_ci			phylink_set(pl->supported, 100000baseKR2_Full);
96962306a36Sopenharmony_ci			phylink_set(pl->supported, 100000baseSR2_Full);
97062306a36Sopenharmony_ci			phylink_set(pl->supported, 100000baseCR2_Full);
97162306a36Sopenharmony_ci			phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
97262306a36Sopenharmony_ci			phylink_set(pl->supported, 100000baseDR2_Full);
97362306a36Sopenharmony_ci			break;
97462306a36Sopenharmony_ci
97562306a36Sopenharmony_ci		default:
97662306a36Sopenharmony_ci			phylink_err(pl,
97762306a36Sopenharmony_ci				    "incorrect link mode %s for in-band status\n",
97862306a36Sopenharmony_ci				    phy_modes(pl->link_config.interface));
97962306a36Sopenharmony_ci			return -EINVAL;
98062306a36Sopenharmony_ci		}
98162306a36Sopenharmony_ci
98262306a36Sopenharmony_ci		linkmode_copy(pl->link_config.advertising, pl->supported);
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci		if (phylink_validate(pl, pl->supported, &pl->link_config)) {
98562306a36Sopenharmony_ci			phylink_err(pl,
98662306a36Sopenharmony_ci				    "failed to validate link configuration for in-band status\n");
98762306a36Sopenharmony_ci			return -EINVAL;
98862306a36Sopenharmony_ci		}
98962306a36Sopenharmony_ci	}
99062306a36Sopenharmony_ci
99162306a36Sopenharmony_ci	return 0;
99262306a36Sopenharmony_ci}
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_cistatic void phylink_apply_manual_flow(struct phylink *pl,
99562306a36Sopenharmony_ci				      struct phylink_link_state *state)
99662306a36Sopenharmony_ci{
99762306a36Sopenharmony_ci	/* If autoneg is disabled, pause AN is also disabled */
99862306a36Sopenharmony_ci	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
99962306a36Sopenharmony_ci			       state->advertising))
100062306a36Sopenharmony_ci		state->pause &= ~MLO_PAUSE_AN;
100162306a36Sopenharmony_ci
100262306a36Sopenharmony_ci	/* Manual configuration of pause modes */
100362306a36Sopenharmony_ci	if (!(pl->link_config.pause & MLO_PAUSE_AN))
100462306a36Sopenharmony_ci		state->pause = pl->link_config.pause;
100562306a36Sopenharmony_ci}
100662306a36Sopenharmony_ci
100762306a36Sopenharmony_cistatic void phylink_resolve_an_pause(struct phylink_link_state *state)
100862306a36Sopenharmony_ci{
100962306a36Sopenharmony_ci	bool tx_pause, rx_pause;
101062306a36Sopenharmony_ci
101162306a36Sopenharmony_ci	if (state->duplex == DUPLEX_FULL) {
101262306a36Sopenharmony_ci		linkmode_resolve_pause(state->advertising,
101362306a36Sopenharmony_ci				       state->lp_advertising,
101462306a36Sopenharmony_ci				       &tx_pause, &rx_pause);
101562306a36Sopenharmony_ci		if (tx_pause)
101662306a36Sopenharmony_ci			state->pause |= MLO_PAUSE_TX;
101762306a36Sopenharmony_ci		if (rx_pause)
101862306a36Sopenharmony_ci			state->pause |= MLO_PAUSE_RX;
101962306a36Sopenharmony_ci	}
102062306a36Sopenharmony_ci}
102162306a36Sopenharmony_ci
102262306a36Sopenharmony_cistatic void phylink_pcs_pre_config(struct phylink_pcs *pcs,
102362306a36Sopenharmony_ci				   phy_interface_t interface)
102462306a36Sopenharmony_ci{
102562306a36Sopenharmony_ci	if (pcs && pcs->ops->pcs_pre_config)
102662306a36Sopenharmony_ci		pcs->ops->pcs_pre_config(pcs, interface);
102762306a36Sopenharmony_ci}
102862306a36Sopenharmony_ci
102962306a36Sopenharmony_cistatic int phylink_pcs_post_config(struct phylink_pcs *pcs,
103062306a36Sopenharmony_ci				   phy_interface_t interface)
103162306a36Sopenharmony_ci{
103262306a36Sopenharmony_ci	int err = 0;
103362306a36Sopenharmony_ci
103462306a36Sopenharmony_ci	if (pcs && pcs->ops->pcs_post_config)
103562306a36Sopenharmony_ci		err = pcs->ops->pcs_post_config(pcs, interface);
103662306a36Sopenharmony_ci
103762306a36Sopenharmony_ci	return err;
103862306a36Sopenharmony_ci}
103962306a36Sopenharmony_ci
104062306a36Sopenharmony_cistatic void phylink_pcs_disable(struct phylink_pcs *pcs)
104162306a36Sopenharmony_ci{
104262306a36Sopenharmony_ci	if (pcs && pcs->ops->pcs_disable)
104362306a36Sopenharmony_ci		pcs->ops->pcs_disable(pcs);
104462306a36Sopenharmony_ci}
104562306a36Sopenharmony_ci
104662306a36Sopenharmony_cistatic int phylink_pcs_enable(struct phylink_pcs *pcs)
104762306a36Sopenharmony_ci{
104862306a36Sopenharmony_ci	int err = 0;
104962306a36Sopenharmony_ci
105062306a36Sopenharmony_ci	if (pcs && pcs->ops->pcs_enable)
105162306a36Sopenharmony_ci		err = pcs->ops->pcs_enable(pcs);
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci	return err;
105462306a36Sopenharmony_ci}
105562306a36Sopenharmony_ci
105662306a36Sopenharmony_cistatic int phylink_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
105762306a36Sopenharmony_ci			      const struct phylink_link_state *state,
105862306a36Sopenharmony_ci			      bool permit_pause_to_mac)
105962306a36Sopenharmony_ci{
106062306a36Sopenharmony_ci	if (!pcs)
106162306a36Sopenharmony_ci		return 0;
106262306a36Sopenharmony_ci
106362306a36Sopenharmony_ci	return pcs->ops->pcs_config(pcs, neg_mode, state->interface,
106462306a36Sopenharmony_ci				    state->advertising, permit_pause_to_mac);
106562306a36Sopenharmony_ci}
106662306a36Sopenharmony_ci
106762306a36Sopenharmony_cistatic void phylink_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
106862306a36Sopenharmony_ci				phy_interface_t interface, int speed,
106962306a36Sopenharmony_ci				int duplex)
107062306a36Sopenharmony_ci{
107162306a36Sopenharmony_ci	if (pcs && pcs->ops->pcs_link_up)
107262306a36Sopenharmony_ci		pcs->ops->pcs_link_up(pcs, neg_mode, interface, speed, duplex);
107362306a36Sopenharmony_ci}
107462306a36Sopenharmony_ci
107562306a36Sopenharmony_cistatic void phylink_pcs_poll_stop(struct phylink *pl)
107662306a36Sopenharmony_ci{
107762306a36Sopenharmony_ci	if (pl->cfg_link_an_mode == MLO_AN_INBAND)
107862306a36Sopenharmony_ci		del_timer(&pl->link_poll);
107962306a36Sopenharmony_ci}
108062306a36Sopenharmony_ci
108162306a36Sopenharmony_cistatic void phylink_pcs_poll_start(struct phylink *pl)
108262306a36Sopenharmony_ci{
108362306a36Sopenharmony_ci	if (pl->pcs && pl->pcs->poll && pl->cfg_link_an_mode == MLO_AN_INBAND)
108462306a36Sopenharmony_ci		mod_timer(&pl->link_poll, jiffies + HZ);
108562306a36Sopenharmony_ci}
108662306a36Sopenharmony_ci
108762306a36Sopenharmony_cistatic void phylink_mac_config(struct phylink *pl,
108862306a36Sopenharmony_ci			       const struct phylink_link_state *state)
108962306a36Sopenharmony_ci{
109062306a36Sopenharmony_ci	struct phylink_link_state st = *state;
109162306a36Sopenharmony_ci
109262306a36Sopenharmony_ci	/* Stop drivers incorrectly using these */
109362306a36Sopenharmony_ci	linkmode_zero(st.lp_advertising);
109462306a36Sopenharmony_ci	st.speed = SPEED_UNKNOWN;
109562306a36Sopenharmony_ci	st.duplex = DUPLEX_UNKNOWN;
109662306a36Sopenharmony_ci	st.an_complete = false;
109762306a36Sopenharmony_ci	st.link = false;
109862306a36Sopenharmony_ci
109962306a36Sopenharmony_ci	phylink_dbg(pl,
110062306a36Sopenharmony_ci		    "%s: mode=%s/%s/%s adv=%*pb pause=%02x\n",
110162306a36Sopenharmony_ci		    __func__, phylink_an_mode_str(pl->cur_link_an_mode),
110262306a36Sopenharmony_ci		    phy_modes(st.interface),
110362306a36Sopenharmony_ci		    phy_rate_matching_to_str(st.rate_matching),
110462306a36Sopenharmony_ci		    __ETHTOOL_LINK_MODE_MASK_NBITS, st.advertising,
110562306a36Sopenharmony_ci		    st.pause);
110662306a36Sopenharmony_ci
110762306a36Sopenharmony_ci	pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, &st);
110862306a36Sopenharmony_ci}
110962306a36Sopenharmony_ci
111062306a36Sopenharmony_cistatic void phylink_pcs_an_restart(struct phylink *pl)
111162306a36Sopenharmony_ci{
111262306a36Sopenharmony_ci	if (pl->pcs && linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
111362306a36Sopenharmony_ci					 pl->link_config.advertising) &&
111462306a36Sopenharmony_ci	    phy_interface_mode_is_8023z(pl->link_config.interface) &&
111562306a36Sopenharmony_ci	    phylink_autoneg_inband(pl->cur_link_an_mode))
111662306a36Sopenharmony_ci		pl->pcs->ops->pcs_an_restart(pl->pcs);
111762306a36Sopenharmony_ci}
111862306a36Sopenharmony_ci
111962306a36Sopenharmony_cistatic void phylink_major_config(struct phylink *pl, bool restart,
112062306a36Sopenharmony_ci				  const struct phylink_link_state *state)
112162306a36Sopenharmony_ci{
112262306a36Sopenharmony_ci	struct phylink_pcs *pcs = NULL;
112362306a36Sopenharmony_ci	bool pcs_changed = false;
112462306a36Sopenharmony_ci	unsigned int rate_kbd;
112562306a36Sopenharmony_ci	unsigned int neg_mode;
112662306a36Sopenharmony_ci	int err;
112762306a36Sopenharmony_ci
112862306a36Sopenharmony_ci	phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
112962306a36Sopenharmony_ci
113062306a36Sopenharmony_ci	pl->pcs_neg_mode = phylink_pcs_neg_mode(pl->cur_link_an_mode,
113162306a36Sopenharmony_ci						state->interface,
113262306a36Sopenharmony_ci						state->advertising);
113362306a36Sopenharmony_ci
113462306a36Sopenharmony_ci	if (pl->using_mac_select_pcs) {
113562306a36Sopenharmony_ci		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
113662306a36Sopenharmony_ci		if (IS_ERR(pcs)) {
113762306a36Sopenharmony_ci			phylink_err(pl,
113862306a36Sopenharmony_ci				    "mac_select_pcs unexpectedly failed: %pe\n",
113962306a36Sopenharmony_ci				    pcs);
114062306a36Sopenharmony_ci			return;
114162306a36Sopenharmony_ci		}
114262306a36Sopenharmony_ci
114362306a36Sopenharmony_ci		pcs_changed = pcs && pl->pcs != pcs;
114462306a36Sopenharmony_ci	}
114562306a36Sopenharmony_ci
114662306a36Sopenharmony_ci	phylink_pcs_poll_stop(pl);
114762306a36Sopenharmony_ci
114862306a36Sopenharmony_ci	if (pl->mac_ops->mac_prepare) {
114962306a36Sopenharmony_ci		err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
115062306a36Sopenharmony_ci					       state->interface);
115162306a36Sopenharmony_ci		if (err < 0) {
115262306a36Sopenharmony_ci			phylink_err(pl, "mac_prepare failed: %pe\n",
115362306a36Sopenharmony_ci				    ERR_PTR(err));
115462306a36Sopenharmony_ci			return;
115562306a36Sopenharmony_ci		}
115662306a36Sopenharmony_ci	}
115762306a36Sopenharmony_ci
115862306a36Sopenharmony_ci	/* If we have a new PCS, switch to the new PCS after preparing the MAC
115962306a36Sopenharmony_ci	 * for the change.
116062306a36Sopenharmony_ci	 */
116162306a36Sopenharmony_ci	if (pcs_changed) {
116262306a36Sopenharmony_ci		phylink_pcs_disable(pl->pcs);
116362306a36Sopenharmony_ci
116462306a36Sopenharmony_ci		if (pl->pcs)
116562306a36Sopenharmony_ci			pl->pcs->phylink = NULL;
116662306a36Sopenharmony_ci
116762306a36Sopenharmony_ci		pcs->phylink = pl;
116862306a36Sopenharmony_ci
116962306a36Sopenharmony_ci		pl->pcs = pcs;
117062306a36Sopenharmony_ci	}
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci	if (pl->pcs)
117362306a36Sopenharmony_ci		phylink_pcs_pre_config(pl->pcs, state->interface);
117462306a36Sopenharmony_ci
117562306a36Sopenharmony_ci	phylink_mac_config(pl, state);
117662306a36Sopenharmony_ci
117762306a36Sopenharmony_ci	if (pl->pcs)
117862306a36Sopenharmony_ci		phylink_pcs_post_config(pl->pcs, state->interface);
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_ci	if (pl->pcs_state == PCS_STATE_STARTING || pcs_changed)
118162306a36Sopenharmony_ci		phylink_pcs_enable(pl->pcs);
118262306a36Sopenharmony_ci
118362306a36Sopenharmony_ci	neg_mode = pl->cur_link_an_mode;
118462306a36Sopenharmony_ci	if (pl->pcs && pl->pcs->neg_mode)
118562306a36Sopenharmony_ci		neg_mode = pl->pcs_neg_mode;
118662306a36Sopenharmony_ci
118762306a36Sopenharmony_ci	err = phylink_pcs_config(pl->pcs, neg_mode, state,
118862306a36Sopenharmony_ci				 !!(pl->link_config.pause & MLO_PAUSE_AN));
118962306a36Sopenharmony_ci	if (err < 0)
119062306a36Sopenharmony_ci		phylink_err(pl, "pcs_config failed: %pe\n",
119162306a36Sopenharmony_ci			    ERR_PTR(err));
119262306a36Sopenharmony_ci	else if (err > 0)
119362306a36Sopenharmony_ci		restart = true;
119462306a36Sopenharmony_ci
119562306a36Sopenharmony_ci	if (restart)
119662306a36Sopenharmony_ci		phylink_pcs_an_restart(pl);
119762306a36Sopenharmony_ci
119862306a36Sopenharmony_ci	if (pl->mac_ops->mac_finish) {
119962306a36Sopenharmony_ci		err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
120062306a36Sopenharmony_ci					      state->interface);
120162306a36Sopenharmony_ci		if (err < 0)
120262306a36Sopenharmony_ci			phylink_err(pl, "mac_finish failed: %pe\n",
120362306a36Sopenharmony_ci				    ERR_PTR(err));
120462306a36Sopenharmony_ci	}
120562306a36Sopenharmony_ci
120662306a36Sopenharmony_ci	if (pl->sfp_bus) {
120762306a36Sopenharmony_ci		rate_kbd = phylink_interface_signal_rate(state->interface);
120862306a36Sopenharmony_ci		if (rate_kbd)
120962306a36Sopenharmony_ci			sfp_upstream_set_signal_rate(pl->sfp_bus, rate_kbd);
121062306a36Sopenharmony_ci	}
121162306a36Sopenharmony_ci
121262306a36Sopenharmony_ci	phylink_pcs_poll_start(pl);
121362306a36Sopenharmony_ci}
121462306a36Sopenharmony_ci
121562306a36Sopenharmony_ci/*
121662306a36Sopenharmony_ci * Reconfigure for a change of inband advertisement.
121762306a36Sopenharmony_ci * If we have a separate PCS, we only need to call its pcs_config() method,
121862306a36Sopenharmony_ci * and then restart AN if it indicates something changed. Otherwise, we do
121962306a36Sopenharmony_ci * the full MAC reconfiguration.
122062306a36Sopenharmony_ci */
122162306a36Sopenharmony_cistatic int phylink_change_inband_advert(struct phylink *pl)
122262306a36Sopenharmony_ci{
122362306a36Sopenharmony_ci	unsigned int neg_mode;
122462306a36Sopenharmony_ci	int ret;
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
122762306a36Sopenharmony_ci		return 0;
122862306a36Sopenharmony_ci
122962306a36Sopenharmony_ci	phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
123062306a36Sopenharmony_ci		    phylink_an_mode_str(pl->cur_link_an_mode),
123162306a36Sopenharmony_ci		    phy_modes(pl->link_config.interface),
123262306a36Sopenharmony_ci		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
123362306a36Sopenharmony_ci		    pl->link_config.pause);
123462306a36Sopenharmony_ci
123562306a36Sopenharmony_ci	/* Recompute the PCS neg mode */
123662306a36Sopenharmony_ci	pl->pcs_neg_mode = phylink_pcs_neg_mode(pl->cur_link_an_mode,
123762306a36Sopenharmony_ci					pl->link_config.interface,
123862306a36Sopenharmony_ci					pl->link_config.advertising);
123962306a36Sopenharmony_ci
124062306a36Sopenharmony_ci	neg_mode = pl->cur_link_an_mode;
124162306a36Sopenharmony_ci	if (pl->pcs->neg_mode)
124262306a36Sopenharmony_ci		neg_mode = pl->pcs_neg_mode;
124362306a36Sopenharmony_ci
124462306a36Sopenharmony_ci	/* Modern PCS-based method; update the advert at the PCS, and
124562306a36Sopenharmony_ci	 * restart negotiation if the pcs_config() helper indicates that
124662306a36Sopenharmony_ci	 * the programmed advertisement has changed.
124762306a36Sopenharmony_ci	 */
124862306a36Sopenharmony_ci	ret = phylink_pcs_config(pl->pcs, neg_mode, &pl->link_config,
124962306a36Sopenharmony_ci				 !!(pl->link_config.pause & MLO_PAUSE_AN));
125062306a36Sopenharmony_ci	if (ret < 0)
125162306a36Sopenharmony_ci		return ret;
125262306a36Sopenharmony_ci
125362306a36Sopenharmony_ci	if (ret > 0)
125462306a36Sopenharmony_ci		phylink_pcs_an_restart(pl);
125562306a36Sopenharmony_ci
125662306a36Sopenharmony_ci	return 0;
125762306a36Sopenharmony_ci}
125862306a36Sopenharmony_ci
125962306a36Sopenharmony_cistatic void phylink_mac_pcs_get_state(struct phylink *pl,
126062306a36Sopenharmony_ci				      struct phylink_link_state *state)
126162306a36Sopenharmony_ci{
126262306a36Sopenharmony_ci	linkmode_copy(state->advertising, pl->link_config.advertising);
126362306a36Sopenharmony_ci	linkmode_zero(state->lp_advertising);
126462306a36Sopenharmony_ci	state->interface = pl->link_config.interface;
126562306a36Sopenharmony_ci	state->rate_matching = pl->link_config.rate_matching;
126662306a36Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
126762306a36Sopenharmony_ci			      state->advertising)) {
126862306a36Sopenharmony_ci		state->speed = SPEED_UNKNOWN;
126962306a36Sopenharmony_ci		state->duplex = DUPLEX_UNKNOWN;
127062306a36Sopenharmony_ci		state->pause = MLO_PAUSE_NONE;
127162306a36Sopenharmony_ci	} else {
127262306a36Sopenharmony_ci		state->speed =  pl->link_config.speed;
127362306a36Sopenharmony_ci		state->duplex = pl->link_config.duplex;
127462306a36Sopenharmony_ci		state->pause = pl->link_config.pause;
127562306a36Sopenharmony_ci	}
127662306a36Sopenharmony_ci	state->an_complete = 0;
127762306a36Sopenharmony_ci	state->link = 1;
127862306a36Sopenharmony_ci
127962306a36Sopenharmony_ci	if (pl->pcs)
128062306a36Sopenharmony_ci		pl->pcs->ops->pcs_get_state(pl->pcs, state);
128162306a36Sopenharmony_ci	else
128262306a36Sopenharmony_ci		state->link = 0;
128362306a36Sopenharmony_ci}
128462306a36Sopenharmony_ci
128562306a36Sopenharmony_ci/* The fixed state is... fixed except for the link state,
128662306a36Sopenharmony_ci * which may be determined by a GPIO or a callback.
128762306a36Sopenharmony_ci */
128862306a36Sopenharmony_cistatic void phylink_get_fixed_state(struct phylink *pl,
128962306a36Sopenharmony_ci				    struct phylink_link_state *state)
129062306a36Sopenharmony_ci{
129162306a36Sopenharmony_ci	*state = pl->link_config;
129262306a36Sopenharmony_ci	if (pl->config->get_fixed_state)
129362306a36Sopenharmony_ci		pl->config->get_fixed_state(pl->config, state);
129462306a36Sopenharmony_ci	else if (pl->link_gpio)
129562306a36Sopenharmony_ci		state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
129662306a36Sopenharmony_ci
129762306a36Sopenharmony_ci	state->pause = MLO_PAUSE_NONE;
129862306a36Sopenharmony_ci	phylink_resolve_an_pause(state);
129962306a36Sopenharmony_ci}
130062306a36Sopenharmony_ci
130162306a36Sopenharmony_cistatic void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
130262306a36Sopenharmony_ci{
130362306a36Sopenharmony_ci	struct phylink_link_state link_state;
130462306a36Sopenharmony_ci
130562306a36Sopenharmony_ci	switch (pl->cur_link_an_mode) {
130662306a36Sopenharmony_ci	case MLO_AN_PHY:
130762306a36Sopenharmony_ci		link_state = pl->phy_state;
130862306a36Sopenharmony_ci		break;
130962306a36Sopenharmony_ci
131062306a36Sopenharmony_ci	case MLO_AN_FIXED:
131162306a36Sopenharmony_ci		phylink_get_fixed_state(pl, &link_state);
131262306a36Sopenharmony_ci		break;
131362306a36Sopenharmony_ci
131462306a36Sopenharmony_ci	case MLO_AN_INBAND:
131562306a36Sopenharmony_ci		link_state = pl->link_config;
131662306a36Sopenharmony_ci		if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
131762306a36Sopenharmony_ci			link_state.pause = MLO_PAUSE_NONE;
131862306a36Sopenharmony_ci		break;
131962306a36Sopenharmony_ci
132062306a36Sopenharmony_ci	default: /* can't happen */
132162306a36Sopenharmony_ci		return;
132262306a36Sopenharmony_ci	}
132362306a36Sopenharmony_ci
132462306a36Sopenharmony_ci	link_state.link = false;
132562306a36Sopenharmony_ci
132662306a36Sopenharmony_ci	phylink_apply_manual_flow(pl, &link_state);
132762306a36Sopenharmony_ci	phylink_major_config(pl, force_restart, &link_state);
132862306a36Sopenharmony_ci}
132962306a36Sopenharmony_ci
133062306a36Sopenharmony_cistatic const char *phylink_pause_to_str(int pause)
133162306a36Sopenharmony_ci{
133262306a36Sopenharmony_ci	switch (pause & MLO_PAUSE_TXRX_MASK) {
133362306a36Sopenharmony_ci	case MLO_PAUSE_TX | MLO_PAUSE_RX:
133462306a36Sopenharmony_ci		return "rx/tx";
133562306a36Sopenharmony_ci	case MLO_PAUSE_TX:
133662306a36Sopenharmony_ci		return "tx";
133762306a36Sopenharmony_ci	case MLO_PAUSE_RX:
133862306a36Sopenharmony_ci		return "rx";
133962306a36Sopenharmony_ci	default:
134062306a36Sopenharmony_ci		return "off";
134162306a36Sopenharmony_ci	}
134262306a36Sopenharmony_ci}
134362306a36Sopenharmony_ci
134462306a36Sopenharmony_cistatic void phylink_link_up(struct phylink *pl,
134562306a36Sopenharmony_ci			    struct phylink_link_state link_state)
134662306a36Sopenharmony_ci{
134762306a36Sopenharmony_ci	struct net_device *ndev = pl->netdev;
134862306a36Sopenharmony_ci	unsigned int neg_mode;
134962306a36Sopenharmony_ci	int speed, duplex;
135062306a36Sopenharmony_ci	bool rx_pause;
135162306a36Sopenharmony_ci
135262306a36Sopenharmony_ci	speed = link_state.speed;
135362306a36Sopenharmony_ci	duplex = link_state.duplex;
135462306a36Sopenharmony_ci	rx_pause = !!(link_state.pause & MLO_PAUSE_RX);
135562306a36Sopenharmony_ci
135662306a36Sopenharmony_ci	switch (link_state.rate_matching) {
135762306a36Sopenharmony_ci	case RATE_MATCH_PAUSE:
135862306a36Sopenharmony_ci		/* The PHY is doing rate matchion from the media rate (in
135962306a36Sopenharmony_ci		 * the link_state) to the interface speed, and will send
136062306a36Sopenharmony_ci		 * pause frames to the MAC to limit its transmission speed.
136162306a36Sopenharmony_ci		 */
136262306a36Sopenharmony_ci		speed = phylink_interface_max_speed(link_state.interface);
136362306a36Sopenharmony_ci		duplex = DUPLEX_FULL;
136462306a36Sopenharmony_ci		rx_pause = true;
136562306a36Sopenharmony_ci		break;
136662306a36Sopenharmony_ci
136762306a36Sopenharmony_ci	case RATE_MATCH_CRS:
136862306a36Sopenharmony_ci		/* The PHY is doing rate matchion from the media rate (in
136962306a36Sopenharmony_ci		 * the link_state) to the interface speed, and will cause
137062306a36Sopenharmony_ci		 * collisions to the MAC to limit its transmission speed.
137162306a36Sopenharmony_ci		 */
137262306a36Sopenharmony_ci		speed = phylink_interface_max_speed(link_state.interface);
137362306a36Sopenharmony_ci		duplex = DUPLEX_HALF;
137462306a36Sopenharmony_ci		break;
137562306a36Sopenharmony_ci	}
137662306a36Sopenharmony_ci
137762306a36Sopenharmony_ci	pl->cur_interface = link_state.interface;
137862306a36Sopenharmony_ci
137962306a36Sopenharmony_ci	neg_mode = pl->cur_link_an_mode;
138062306a36Sopenharmony_ci	if (pl->pcs && pl->pcs->neg_mode)
138162306a36Sopenharmony_ci		neg_mode = pl->pcs_neg_mode;
138262306a36Sopenharmony_ci
138362306a36Sopenharmony_ci	phylink_pcs_link_up(pl->pcs, neg_mode, pl->cur_interface, speed,
138462306a36Sopenharmony_ci			    duplex);
138562306a36Sopenharmony_ci
138662306a36Sopenharmony_ci	pl->mac_ops->mac_link_up(pl->config, pl->phydev, pl->cur_link_an_mode,
138762306a36Sopenharmony_ci				 pl->cur_interface, speed, duplex,
138862306a36Sopenharmony_ci				 !!(link_state.pause & MLO_PAUSE_TX), rx_pause);
138962306a36Sopenharmony_ci
139062306a36Sopenharmony_ci	if (ndev)
139162306a36Sopenharmony_ci		netif_carrier_on(ndev);
139262306a36Sopenharmony_ci
139362306a36Sopenharmony_ci	phylink_info(pl,
139462306a36Sopenharmony_ci		     "Link is Up - %s/%s - flow control %s\n",
139562306a36Sopenharmony_ci		     phy_speed_to_str(link_state.speed),
139662306a36Sopenharmony_ci		     phy_duplex_to_str(link_state.duplex),
139762306a36Sopenharmony_ci		     phylink_pause_to_str(link_state.pause));
139862306a36Sopenharmony_ci}
139962306a36Sopenharmony_ci
140062306a36Sopenharmony_cistatic void phylink_link_down(struct phylink *pl)
140162306a36Sopenharmony_ci{
140262306a36Sopenharmony_ci	struct net_device *ndev = pl->netdev;
140362306a36Sopenharmony_ci
140462306a36Sopenharmony_ci	if (ndev)
140562306a36Sopenharmony_ci		netif_carrier_off(ndev);
140662306a36Sopenharmony_ci	pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
140762306a36Sopenharmony_ci				   pl->cur_interface);
140862306a36Sopenharmony_ci	phylink_info(pl, "Link is Down\n");
140962306a36Sopenharmony_ci}
141062306a36Sopenharmony_ci
141162306a36Sopenharmony_cistatic void phylink_resolve(struct work_struct *w)
141262306a36Sopenharmony_ci{
141362306a36Sopenharmony_ci	struct phylink *pl = container_of(w, struct phylink, resolve);
141462306a36Sopenharmony_ci	struct phylink_link_state link_state;
141562306a36Sopenharmony_ci	struct net_device *ndev = pl->netdev;
141662306a36Sopenharmony_ci	bool mac_config = false;
141762306a36Sopenharmony_ci	bool retrigger = false;
141862306a36Sopenharmony_ci	bool cur_link_state;
141962306a36Sopenharmony_ci
142062306a36Sopenharmony_ci	mutex_lock(&pl->state_mutex);
142162306a36Sopenharmony_ci	if (pl->netdev)
142262306a36Sopenharmony_ci		cur_link_state = netif_carrier_ok(ndev);
142362306a36Sopenharmony_ci	else
142462306a36Sopenharmony_ci		cur_link_state = pl->old_link_state;
142562306a36Sopenharmony_ci
142662306a36Sopenharmony_ci	if (pl->phylink_disable_state) {
142762306a36Sopenharmony_ci		pl->mac_link_dropped = false;
142862306a36Sopenharmony_ci		link_state.link = false;
142962306a36Sopenharmony_ci	} else if (pl->mac_link_dropped) {
143062306a36Sopenharmony_ci		link_state.link = false;
143162306a36Sopenharmony_ci		retrigger = true;
143262306a36Sopenharmony_ci	} else {
143362306a36Sopenharmony_ci		switch (pl->cur_link_an_mode) {
143462306a36Sopenharmony_ci		case MLO_AN_PHY:
143562306a36Sopenharmony_ci			link_state = pl->phy_state;
143662306a36Sopenharmony_ci			phylink_apply_manual_flow(pl, &link_state);
143762306a36Sopenharmony_ci			mac_config = link_state.link;
143862306a36Sopenharmony_ci			break;
143962306a36Sopenharmony_ci
144062306a36Sopenharmony_ci		case MLO_AN_FIXED:
144162306a36Sopenharmony_ci			phylink_get_fixed_state(pl, &link_state);
144262306a36Sopenharmony_ci			mac_config = link_state.link;
144362306a36Sopenharmony_ci			break;
144462306a36Sopenharmony_ci
144562306a36Sopenharmony_ci		case MLO_AN_INBAND:
144662306a36Sopenharmony_ci			phylink_mac_pcs_get_state(pl, &link_state);
144762306a36Sopenharmony_ci
144862306a36Sopenharmony_ci			/* The PCS may have a latching link-fail indicator.
144962306a36Sopenharmony_ci			 * If the link was up, bring the link down and
145062306a36Sopenharmony_ci			 * re-trigger the resolve. Otherwise, re-read the
145162306a36Sopenharmony_ci			 * PCS state to get the current status of the link.
145262306a36Sopenharmony_ci			 */
145362306a36Sopenharmony_ci			if (!link_state.link) {
145462306a36Sopenharmony_ci				if (cur_link_state)
145562306a36Sopenharmony_ci					retrigger = true;
145662306a36Sopenharmony_ci				else
145762306a36Sopenharmony_ci					phylink_mac_pcs_get_state(pl,
145862306a36Sopenharmony_ci								  &link_state);
145962306a36Sopenharmony_ci			}
146062306a36Sopenharmony_ci
146162306a36Sopenharmony_ci			/* If we have a phy, the "up" state is the union of
146262306a36Sopenharmony_ci			 * both the PHY and the MAC
146362306a36Sopenharmony_ci			 */
146462306a36Sopenharmony_ci			if (pl->phydev)
146562306a36Sopenharmony_ci				link_state.link &= pl->phy_state.link;
146662306a36Sopenharmony_ci
146762306a36Sopenharmony_ci			/* Only update if the PHY link is up */
146862306a36Sopenharmony_ci			if (pl->phydev && pl->phy_state.link) {
146962306a36Sopenharmony_ci				/* If the interface has changed, force a
147062306a36Sopenharmony_ci				 * link down event if the link isn't already
147162306a36Sopenharmony_ci				 * down, and re-resolve.
147262306a36Sopenharmony_ci				 */
147362306a36Sopenharmony_ci				if (link_state.interface !=
147462306a36Sopenharmony_ci				    pl->phy_state.interface) {
147562306a36Sopenharmony_ci					retrigger = true;
147662306a36Sopenharmony_ci					link_state.link = false;
147762306a36Sopenharmony_ci				}
147862306a36Sopenharmony_ci				link_state.interface = pl->phy_state.interface;
147962306a36Sopenharmony_ci
148062306a36Sopenharmony_ci				/* If we are doing rate matching, then the
148162306a36Sopenharmony_ci				 * link speed/duplex comes from the PHY
148262306a36Sopenharmony_ci				 */
148362306a36Sopenharmony_ci				if (pl->phy_state.rate_matching) {
148462306a36Sopenharmony_ci					link_state.rate_matching =
148562306a36Sopenharmony_ci						pl->phy_state.rate_matching;
148662306a36Sopenharmony_ci					link_state.speed = pl->phy_state.speed;
148762306a36Sopenharmony_ci					link_state.duplex =
148862306a36Sopenharmony_ci						pl->phy_state.duplex;
148962306a36Sopenharmony_ci				}
149062306a36Sopenharmony_ci
149162306a36Sopenharmony_ci				/* If we have a PHY, we need to update with
149262306a36Sopenharmony_ci				 * the PHY flow control bits.
149362306a36Sopenharmony_ci				 */
149462306a36Sopenharmony_ci				link_state.pause = pl->phy_state.pause;
149562306a36Sopenharmony_ci				mac_config = true;
149662306a36Sopenharmony_ci			}
149762306a36Sopenharmony_ci			phylink_apply_manual_flow(pl, &link_state);
149862306a36Sopenharmony_ci			break;
149962306a36Sopenharmony_ci		}
150062306a36Sopenharmony_ci	}
150162306a36Sopenharmony_ci
150262306a36Sopenharmony_ci	if (mac_config) {
150362306a36Sopenharmony_ci		if (link_state.interface != pl->link_config.interface) {
150462306a36Sopenharmony_ci			/* The interface has changed, force the link down and
150562306a36Sopenharmony_ci			 * then reconfigure.
150662306a36Sopenharmony_ci			 */
150762306a36Sopenharmony_ci			if (cur_link_state) {
150862306a36Sopenharmony_ci				phylink_link_down(pl);
150962306a36Sopenharmony_ci				cur_link_state = false;
151062306a36Sopenharmony_ci			}
151162306a36Sopenharmony_ci			phylink_major_config(pl, false, &link_state);
151262306a36Sopenharmony_ci			pl->link_config.interface = link_state.interface;
151362306a36Sopenharmony_ci		}
151462306a36Sopenharmony_ci	}
151562306a36Sopenharmony_ci
151662306a36Sopenharmony_ci	if (link_state.link != cur_link_state) {
151762306a36Sopenharmony_ci		pl->old_link_state = link_state.link;
151862306a36Sopenharmony_ci		if (!link_state.link)
151962306a36Sopenharmony_ci			phylink_link_down(pl);
152062306a36Sopenharmony_ci		else
152162306a36Sopenharmony_ci			phylink_link_up(pl, link_state);
152262306a36Sopenharmony_ci	}
152362306a36Sopenharmony_ci	if (!link_state.link && retrigger) {
152462306a36Sopenharmony_ci		pl->mac_link_dropped = false;
152562306a36Sopenharmony_ci		queue_work(system_power_efficient_wq, &pl->resolve);
152662306a36Sopenharmony_ci	}
152762306a36Sopenharmony_ci	mutex_unlock(&pl->state_mutex);
152862306a36Sopenharmony_ci}
152962306a36Sopenharmony_ci
153062306a36Sopenharmony_cistatic void phylink_run_resolve(struct phylink *pl)
153162306a36Sopenharmony_ci{
153262306a36Sopenharmony_ci	if (!pl->phylink_disable_state)
153362306a36Sopenharmony_ci		queue_work(system_power_efficient_wq, &pl->resolve);
153462306a36Sopenharmony_ci}
153562306a36Sopenharmony_ci
153662306a36Sopenharmony_cistatic void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
153762306a36Sopenharmony_ci{
153862306a36Sopenharmony_ci	unsigned long state = pl->phylink_disable_state;
153962306a36Sopenharmony_ci
154062306a36Sopenharmony_ci	set_bit(bit, &pl->phylink_disable_state);
154162306a36Sopenharmony_ci	if (state == 0) {
154262306a36Sopenharmony_ci		queue_work(system_power_efficient_wq, &pl->resolve);
154362306a36Sopenharmony_ci		flush_work(&pl->resolve);
154462306a36Sopenharmony_ci	}
154562306a36Sopenharmony_ci}
154662306a36Sopenharmony_ci
154762306a36Sopenharmony_cistatic void phylink_enable_and_run_resolve(struct phylink *pl, int bit)
154862306a36Sopenharmony_ci{
154962306a36Sopenharmony_ci	clear_bit(bit, &pl->phylink_disable_state);
155062306a36Sopenharmony_ci	phylink_run_resolve(pl);
155162306a36Sopenharmony_ci}
155262306a36Sopenharmony_ci
155362306a36Sopenharmony_cistatic void phylink_fixed_poll(struct timer_list *t)
155462306a36Sopenharmony_ci{
155562306a36Sopenharmony_ci	struct phylink *pl = container_of(t, struct phylink, link_poll);
155662306a36Sopenharmony_ci
155762306a36Sopenharmony_ci	mod_timer(t, jiffies + HZ);
155862306a36Sopenharmony_ci
155962306a36Sopenharmony_ci	phylink_run_resolve(pl);
156062306a36Sopenharmony_ci}
156162306a36Sopenharmony_ci
156262306a36Sopenharmony_cistatic const struct sfp_upstream_ops sfp_phylink_ops;
156362306a36Sopenharmony_ci
156462306a36Sopenharmony_cistatic int phylink_register_sfp(struct phylink *pl,
156562306a36Sopenharmony_ci				const struct fwnode_handle *fwnode)
156662306a36Sopenharmony_ci{
156762306a36Sopenharmony_ci	struct sfp_bus *bus;
156862306a36Sopenharmony_ci	int ret;
156962306a36Sopenharmony_ci
157062306a36Sopenharmony_ci	if (!fwnode)
157162306a36Sopenharmony_ci		return 0;
157262306a36Sopenharmony_ci
157362306a36Sopenharmony_ci	bus = sfp_bus_find_fwnode(fwnode);
157462306a36Sopenharmony_ci	if (IS_ERR(bus)) {
157562306a36Sopenharmony_ci		phylink_err(pl, "unable to attach SFP bus: %pe\n", bus);
157662306a36Sopenharmony_ci		return PTR_ERR(bus);
157762306a36Sopenharmony_ci	}
157862306a36Sopenharmony_ci
157962306a36Sopenharmony_ci	pl->sfp_bus = bus;
158062306a36Sopenharmony_ci
158162306a36Sopenharmony_ci	ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
158262306a36Sopenharmony_ci	sfp_bus_put(bus);
158362306a36Sopenharmony_ci
158462306a36Sopenharmony_ci	return ret;
158562306a36Sopenharmony_ci}
158662306a36Sopenharmony_ci
158762306a36Sopenharmony_ci/**
158862306a36Sopenharmony_ci * phylink_create() - create a phylink instance
158962306a36Sopenharmony_ci * @config: a pointer to the target &struct phylink_config
159062306a36Sopenharmony_ci * @fwnode: a pointer to a &struct fwnode_handle describing the network
159162306a36Sopenharmony_ci *	interface
159262306a36Sopenharmony_ci * @iface: the desired link mode defined by &typedef phy_interface_t
159362306a36Sopenharmony_ci * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
159462306a36Sopenharmony_ci *
159562306a36Sopenharmony_ci * Create a new phylink instance, and parse the link parameters found in @np.
159662306a36Sopenharmony_ci * This will parse in-band modes, fixed-link or SFP configuration.
159762306a36Sopenharmony_ci *
159862306a36Sopenharmony_ci * Note: the rtnl lock must not be held when calling this function.
159962306a36Sopenharmony_ci *
160062306a36Sopenharmony_ci * Returns a pointer to a &struct phylink, or an error-pointer value. Users
160162306a36Sopenharmony_ci * must use IS_ERR() to check for errors from this function.
160262306a36Sopenharmony_ci */
160362306a36Sopenharmony_cistruct phylink *phylink_create(struct phylink_config *config,
160462306a36Sopenharmony_ci			       const struct fwnode_handle *fwnode,
160562306a36Sopenharmony_ci			       phy_interface_t iface,
160662306a36Sopenharmony_ci			       const struct phylink_mac_ops *mac_ops)
160762306a36Sopenharmony_ci{
160862306a36Sopenharmony_ci	bool using_mac_select_pcs = false;
160962306a36Sopenharmony_ci	struct phylink *pl;
161062306a36Sopenharmony_ci	int ret;
161162306a36Sopenharmony_ci
161262306a36Sopenharmony_ci	/* Validate the supplied configuration */
161362306a36Sopenharmony_ci	if (phy_interface_empty(config->supported_interfaces)) {
161462306a36Sopenharmony_ci		dev_err(config->dev,
161562306a36Sopenharmony_ci			"phylink: error: empty supported_interfaces\n");
161662306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
161762306a36Sopenharmony_ci	}
161862306a36Sopenharmony_ci
161962306a36Sopenharmony_ci	if (mac_ops->mac_select_pcs &&
162062306a36Sopenharmony_ci	    mac_ops->mac_select_pcs(config, PHY_INTERFACE_MODE_NA) !=
162162306a36Sopenharmony_ci	      ERR_PTR(-EOPNOTSUPP))
162262306a36Sopenharmony_ci		using_mac_select_pcs = true;
162362306a36Sopenharmony_ci
162462306a36Sopenharmony_ci	pl = kzalloc(sizeof(*pl), GFP_KERNEL);
162562306a36Sopenharmony_ci	if (!pl)
162662306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
162762306a36Sopenharmony_ci
162862306a36Sopenharmony_ci	mutex_init(&pl->state_mutex);
162962306a36Sopenharmony_ci	INIT_WORK(&pl->resolve, phylink_resolve);
163062306a36Sopenharmony_ci
163162306a36Sopenharmony_ci	pl->config = config;
163262306a36Sopenharmony_ci	if (config->type == PHYLINK_NETDEV) {
163362306a36Sopenharmony_ci		pl->netdev = to_net_dev(config->dev);
163462306a36Sopenharmony_ci		netif_carrier_off(pl->netdev);
163562306a36Sopenharmony_ci	} else if (config->type == PHYLINK_DEV) {
163662306a36Sopenharmony_ci		pl->dev = config->dev;
163762306a36Sopenharmony_ci	} else {
163862306a36Sopenharmony_ci		kfree(pl);
163962306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
164062306a36Sopenharmony_ci	}
164162306a36Sopenharmony_ci
164262306a36Sopenharmony_ci	pl->using_mac_select_pcs = using_mac_select_pcs;
164362306a36Sopenharmony_ci	pl->phy_state.interface = iface;
164462306a36Sopenharmony_ci	pl->link_interface = iface;
164562306a36Sopenharmony_ci	if (iface == PHY_INTERFACE_MODE_MOCA)
164662306a36Sopenharmony_ci		pl->link_port = PORT_BNC;
164762306a36Sopenharmony_ci	else
164862306a36Sopenharmony_ci		pl->link_port = PORT_MII;
164962306a36Sopenharmony_ci	pl->link_config.interface = iface;
165062306a36Sopenharmony_ci	pl->link_config.pause = MLO_PAUSE_AN;
165162306a36Sopenharmony_ci	pl->link_config.speed = SPEED_UNKNOWN;
165262306a36Sopenharmony_ci	pl->link_config.duplex = DUPLEX_UNKNOWN;
165362306a36Sopenharmony_ci	pl->pcs_state = PCS_STATE_DOWN;
165462306a36Sopenharmony_ci	pl->mac_ops = mac_ops;
165562306a36Sopenharmony_ci	__set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
165662306a36Sopenharmony_ci	timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
165762306a36Sopenharmony_ci
165862306a36Sopenharmony_ci	bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
165962306a36Sopenharmony_ci	linkmode_copy(pl->link_config.advertising, pl->supported);
166062306a36Sopenharmony_ci	phylink_validate(pl, pl->supported, &pl->link_config);
166162306a36Sopenharmony_ci
166262306a36Sopenharmony_ci	ret = phylink_parse_mode(pl, fwnode);
166362306a36Sopenharmony_ci	if (ret < 0) {
166462306a36Sopenharmony_ci		kfree(pl);
166562306a36Sopenharmony_ci		return ERR_PTR(ret);
166662306a36Sopenharmony_ci	}
166762306a36Sopenharmony_ci
166862306a36Sopenharmony_ci	if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
166962306a36Sopenharmony_ci		ret = phylink_parse_fixedlink(pl, fwnode);
167062306a36Sopenharmony_ci		if (ret < 0) {
167162306a36Sopenharmony_ci			kfree(pl);
167262306a36Sopenharmony_ci			return ERR_PTR(ret);
167362306a36Sopenharmony_ci		}
167462306a36Sopenharmony_ci	}
167562306a36Sopenharmony_ci
167662306a36Sopenharmony_ci	pl->cur_link_an_mode = pl->cfg_link_an_mode;
167762306a36Sopenharmony_ci
167862306a36Sopenharmony_ci	ret = phylink_register_sfp(pl, fwnode);
167962306a36Sopenharmony_ci	if (ret < 0) {
168062306a36Sopenharmony_ci		kfree(pl);
168162306a36Sopenharmony_ci		return ERR_PTR(ret);
168262306a36Sopenharmony_ci	}
168362306a36Sopenharmony_ci
168462306a36Sopenharmony_ci	return pl;
168562306a36Sopenharmony_ci}
168662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_create);
168762306a36Sopenharmony_ci
168862306a36Sopenharmony_ci/**
168962306a36Sopenharmony_ci * phylink_destroy() - cleanup and destroy the phylink instance
169062306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
169162306a36Sopenharmony_ci *
169262306a36Sopenharmony_ci * Destroy a phylink instance. Any PHY that has been attached must have been
169362306a36Sopenharmony_ci * cleaned up via phylink_disconnect_phy() prior to calling this function.
169462306a36Sopenharmony_ci *
169562306a36Sopenharmony_ci * Note: the rtnl lock must not be held when calling this function.
169662306a36Sopenharmony_ci */
169762306a36Sopenharmony_civoid phylink_destroy(struct phylink *pl)
169862306a36Sopenharmony_ci{
169962306a36Sopenharmony_ci	sfp_bus_del_upstream(pl->sfp_bus);
170062306a36Sopenharmony_ci	if (pl->link_gpio)
170162306a36Sopenharmony_ci		gpiod_put(pl->link_gpio);
170262306a36Sopenharmony_ci
170362306a36Sopenharmony_ci	cancel_work_sync(&pl->resolve);
170462306a36Sopenharmony_ci	kfree(pl);
170562306a36Sopenharmony_ci}
170662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_destroy);
170762306a36Sopenharmony_ci
170862306a36Sopenharmony_ci/**
170962306a36Sopenharmony_ci * phylink_expects_phy() - Determine if phylink expects a phy to be attached
171062306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
171162306a36Sopenharmony_ci *
171262306a36Sopenharmony_ci * When using fixed-link mode, or in-band mode with 1000base-X or 2500base-X,
171362306a36Sopenharmony_ci * no PHY is needed.
171462306a36Sopenharmony_ci *
171562306a36Sopenharmony_ci * Returns true if phylink will be expecting a PHY.
171662306a36Sopenharmony_ci */
171762306a36Sopenharmony_cibool phylink_expects_phy(struct phylink *pl)
171862306a36Sopenharmony_ci{
171962306a36Sopenharmony_ci	if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
172062306a36Sopenharmony_ci	    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
172162306a36Sopenharmony_ci	     phy_interface_mode_is_8023z(pl->link_config.interface)))
172262306a36Sopenharmony_ci		return false;
172362306a36Sopenharmony_ci	return true;
172462306a36Sopenharmony_ci}
172562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_expects_phy);
172662306a36Sopenharmony_ci
172762306a36Sopenharmony_cistatic void phylink_phy_change(struct phy_device *phydev, bool up)
172862306a36Sopenharmony_ci{
172962306a36Sopenharmony_ci	struct phylink *pl = phydev->phylink;
173062306a36Sopenharmony_ci	bool tx_pause, rx_pause;
173162306a36Sopenharmony_ci
173262306a36Sopenharmony_ci	phy_get_pause(phydev, &tx_pause, &rx_pause);
173362306a36Sopenharmony_ci
173462306a36Sopenharmony_ci	mutex_lock(&pl->state_mutex);
173562306a36Sopenharmony_ci	pl->phy_state.speed = phydev->speed;
173662306a36Sopenharmony_ci	pl->phy_state.duplex = phydev->duplex;
173762306a36Sopenharmony_ci	pl->phy_state.rate_matching = phydev->rate_matching;
173862306a36Sopenharmony_ci	pl->phy_state.pause = MLO_PAUSE_NONE;
173962306a36Sopenharmony_ci	if (tx_pause)
174062306a36Sopenharmony_ci		pl->phy_state.pause |= MLO_PAUSE_TX;
174162306a36Sopenharmony_ci	if (rx_pause)
174262306a36Sopenharmony_ci		pl->phy_state.pause |= MLO_PAUSE_RX;
174362306a36Sopenharmony_ci	pl->phy_state.interface = phydev->interface;
174462306a36Sopenharmony_ci	pl->phy_state.link = up;
174562306a36Sopenharmony_ci	mutex_unlock(&pl->state_mutex);
174662306a36Sopenharmony_ci
174762306a36Sopenharmony_ci	phylink_run_resolve(pl);
174862306a36Sopenharmony_ci
174962306a36Sopenharmony_ci	phylink_dbg(pl, "phy link %s %s/%s/%s/%s/%s\n", up ? "up" : "down",
175062306a36Sopenharmony_ci		    phy_modes(phydev->interface),
175162306a36Sopenharmony_ci		    phy_speed_to_str(phydev->speed),
175262306a36Sopenharmony_ci		    phy_duplex_to_str(phydev->duplex),
175362306a36Sopenharmony_ci		    phy_rate_matching_to_str(phydev->rate_matching),
175462306a36Sopenharmony_ci		    phylink_pause_to_str(pl->phy_state.pause));
175562306a36Sopenharmony_ci}
175662306a36Sopenharmony_ci
175762306a36Sopenharmony_cistatic int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
175862306a36Sopenharmony_ci			       phy_interface_t interface)
175962306a36Sopenharmony_ci{
176062306a36Sopenharmony_ci	struct phylink_link_state config;
176162306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
176262306a36Sopenharmony_ci	char *irq_str;
176362306a36Sopenharmony_ci	int ret;
176462306a36Sopenharmony_ci
176562306a36Sopenharmony_ci	/*
176662306a36Sopenharmony_ci	 * This is the new way of dealing with flow control for PHYs,
176762306a36Sopenharmony_ci	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
176862306a36Sopenharmony_ci	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
176962306a36Sopenharmony_ci	 * using our validate call to the MAC, we rely upon the MAC
177062306a36Sopenharmony_ci	 * clearing the bits from both supported and advertising fields.
177162306a36Sopenharmony_ci	 */
177262306a36Sopenharmony_ci	phy_support_asym_pause(phy);
177362306a36Sopenharmony_ci
177462306a36Sopenharmony_ci	memset(&config, 0, sizeof(config));
177562306a36Sopenharmony_ci	linkmode_copy(supported, phy->supported);
177662306a36Sopenharmony_ci	linkmode_copy(config.advertising, phy->advertising);
177762306a36Sopenharmony_ci
177862306a36Sopenharmony_ci	/* Check whether we would use rate matching for the proposed interface
177962306a36Sopenharmony_ci	 * mode.
178062306a36Sopenharmony_ci	 */
178162306a36Sopenharmony_ci	config.rate_matching = phy_get_rate_matching(phy, interface);
178262306a36Sopenharmony_ci
178362306a36Sopenharmony_ci	/* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
178462306a36Sopenharmony_ci	 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
178562306a36Sopenharmony_ci	 * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching
178662306a36Sopenharmony_ci	 * their Serdes is either unnecessary or not reasonable.
178762306a36Sopenharmony_ci	 *
178862306a36Sopenharmony_ci	 * For these which switch interface modes, we really need to know which
178962306a36Sopenharmony_ci	 * interface modes the PHY supports to properly work out which ethtool
179062306a36Sopenharmony_ci	 * linkmodes can be supported. For now, as a work-around, we validate
179162306a36Sopenharmony_ci	 * against all interface modes, which may lead to more ethtool link
179262306a36Sopenharmony_ci	 * modes being advertised than are actually supported.
179362306a36Sopenharmony_ci	 */
179462306a36Sopenharmony_ci	if (phy->is_c45 && config.rate_matching == RATE_MATCH_NONE &&
179562306a36Sopenharmony_ci	    interface != PHY_INTERFACE_MODE_RXAUI &&
179662306a36Sopenharmony_ci	    interface != PHY_INTERFACE_MODE_XAUI &&
179762306a36Sopenharmony_ci	    interface != PHY_INTERFACE_MODE_USXGMII)
179862306a36Sopenharmony_ci		config.interface = PHY_INTERFACE_MODE_NA;
179962306a36Sopenharmony_ci	else
180062306a36Sopenharmony_ci		config.interface = interface;
180162306a36Sopenharmony_ci
180262306a36Sopenharmony_ci	ret = phylink_validate(pl, supported, &config);
180362306a36Sopenharmony_ci	if (ret) {
180462306a36Sopenharmony_ci		phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
180562306a36Sopenharmony_ci			     phy_modes(config.interface),
180662306a36Sopenharmony_ci			     __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
180762306a36Sopenharmony_ci			     __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
180862306a36Sopenharmony_ci			     ERR_PTR(ret));
180962306a36Sopenharmony_ci		return ret;
181062306a36Sopenharmony_ci	}
181162306a36Sopenharmony_ci
181262306a36Sopenharmony_ci	phy->phylink = pl;
181362306a36Sopenharmony_ci	phy->phy_link_change = phylink_phy_change;
181462306a36Sopenharmony_ci
181562306a36Sopenharmony_ci	irq_str = phy_attached_info_irq(phy);
181662306a36Sopenharmony_ci	phylink_info(pl,
181762306a36Sopenharmony_ci		     "PHY [%s] driver [%s] (irq=%s)\n",
181862306a36Sopenharmony_ci		     dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
181962306a36Sopenharmony_ci	kfree(irq_str);
182062306a36Sopenharmony_ci
182162306a36Sopenharmony_ci	mutex_lock(&phy->lock);
182262306a36Sopenharmony_ci	mutex_lock(&pl->state_mutex);
182362306a36Sopenharmony_ci	pl->phydev = phy;
182462306a36Sopenharmony_ci	pl->phy_state.interface = interface;
182562306a36Sopenharmony_ci	pl->phy_state.pause = MLO_PAUSE_NONE;
182662306a36Sopenharmony_ci	pl->phy_state.speed = SPEED_UNKNOWN;
182762306a36Sopenharmony_ci	pl->phy_state.duplex = DUPLEX_UNKNOWN;
182862306a36Sopenharmony_ci	pl->phy_state.rate_matching = RATE_MATCH_NONE;
182962306a36Sopenharmony_ci	linkmode_copy(pl->supported, supported);
183062306a36Sopenharmony_ci	linkmode_copy(pl->link_config.advertising, config.advertising);
183162306a36Sopenharmony_ci
183262306a36Sopenharmony_ci	/* Restrict the phy advertisement according to the MAC support. */
183362306a36Sopenharmony_ci	linkmode_copy(phy->advertising, config.advertising);
183462306a36Sopenharmony_ci	mutex_unlock(&pl->state_mutex);
183562306a36Sopenharmony_ci	mutex_unlock(&phy->lock);
183662306a36Sopenharmony_ci
183762306a36Sopenharmony_ci	phylink_dbg(pl,
183862306a36Sopenharmony_ci		    "phy: %s setting supported %*pb advertising %*pb\n",
183962306a36Sopenharmony_ci		    phy_modes(interface),
184062306a36Sopenharmony_ci		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
184162306a36Sopenharmony_ci		    __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
184262306a36Sopenharmony_ci
184362306a36Sopenharmony_ci	if (phy_interrupt_is_valid(phy))
184462306a36Sopenharmony_ci		phy_request_interrupt(phy);
184562306a36Sopenharmony_ci
184662306a36Sopenharmony_ci	if (pl->config->mac_managed_pm)
184762306a36Sopenharmony_ci		phy->mac_managed_pm = true;
184862306a36Sopenharmony_ci
184962306a36Sopenharmony_ci	return 0;
185062306a36Sopenharmony_ci}
185162306a36Sopenharmony_ci
185262306a36Sopenharmony_cistatic int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
185362306a36Sopenharmony_ci			      phy_interface_t interface)
185462306a36Sopenharmony_ci{
185562306a36Sopenharmony_ci	if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
185662306a36Sopenharmony_ci		    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
185762306a36Sopenharmony_ci		     phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
185862306a36Sopenharmony_ci		return -EINVAL;
185962306a36Sopenharmony_ci
186062306a36Sopenharmony_ci	if (pl->phydev)
186162306a36Sopenharmony_ci		return -EBUSY;
186262306a36Sopenharmony_ci
186362306a36Sopenharmony_ci	return phy_attach_direct(pl->netdev, phy, 0, interface);
186462306a36Sopenharmony_ci}
186562306a36Sopenharmony_ci
186662306a36Sopenharmony_ci/**
186762306a36Sopenharmony_ci * phylink_connect_phy() - connect a PHY to the phylink instance
186862306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
186962306a36Sopenharmony_ci * @phy: a pointer to a &struct phy_device.
187062306a36Sopenharmony_ci *
187162306a36Sopenharmony_ci * Connect @phy to the phylink instance specified by @pl by calling
187262306a36Sopenharmony_ci * phy_attach_direct(). Configure the @phy according to the MAC driver's
187362306a36Sopenharmony_ci * capabilities, start the PHYLIB state machine and enable any interrupts
187462306a36Sopenharmony_ci * that the PHY supports.
187562306a36Sopenharmony_ci *
187662306a36Sopenharmony_ci * This updates the phylink's ethtool supported and advertising link mode
187762306a36Sopenharmony_ci * masks.
187862306a36Sopenharmony_ci *
187962306a36Sopenharmony_ci * Returns 0 on success or a negative errno.
188062306a36Sopenharmony_ci */
188162306a36Sopenharmony_ciint phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
188262306a36Sopenharmony_ci{
188362306a36Sopenharmony_ci	int ret;
188462306a36Sopenharmony_ci
188562306a36Sopenharmony_ci	/* Use PHY device/driver interface */
188662306a36Sopenharmony_ci	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
188762306a36Sopenharmony_ci		pl->link_interface = phy->interface;
188862306a36Sopenharmony_ci		pl->link_config.interface = pl->link_interface;
188962306a36Sopenharmony_ci	}
189062306a36Sopenharmony_ci
189162306a36Sopenharmony_ci	ret = phylink_attach_phy(pl, phy, pl->link_interface);
189262306a36Sopenharmony_ci	if (ret < 0)
189362306a36Sopenharmony_ci		return ret;
189462306a36Sopenharmony_ci
189562306a36Sopenharmony_ci	ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
189662306a36Sopenharmony_ci	if (ret)
189762306a36Sopenharmony_ci		phy_detach(phy);
189862306a36Sopenharmony_ci
189962306a36Sopenharmony_ci	return ret;
190062306a36Sopenharmony_ci}
190162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_connect_phy);
190262306a36Sopenharmony_ci
190362306a36Sopenharmony_ci/**
190462306a36Sopenharmony_ci * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
190562306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
190662306a36Sopenharmony_ci * @dn: a pointer to a &struct device_node.
190762306a36Sopenharmony_ci * @flags: PHY-specific flags to communicate to the PHY device driver
190862306a36Sopenharmony_ci *
190962306a36Sopenharmony_ci * Connect the phy specified in the device node @dn to the phylink instance
191062306a36Sopenharmony_ci * specified by @pl. Actions specified in phylink_connect_phy() will be
191162306a36Sopenharmony_ci * performed.
191262306a36Sopenharmony_ci *
191362306a36Sopenharmony_ci * Returns 0 on success or a negative errno.
191462306a36Sopenharmony_ci */
191562306a36Sopenharmony_ciint phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
191662306a36Sopenharmony_ci			   u32 flags)
191762306a36Sopenharmony_ci{
191862306a36Sopenharmony_ci	return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
191962306a36Sopenharmony_ci}
192062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_of_phy_connect);
192162306a36Sopenharmony_ci
192262306a36Sopenharmony_ci/**
192362306a36Sopenharmony_ci * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
192462306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
192562306a36Sopenharmony_ci * @fwnode: a pointer to a &struct fwnode_handle.
192662306a36Sopenharmony_ci * @flags: PHY-specific flags to communicate to the PHY device driver
192762306a36Sopenharmony_ci *
192862306a36Sopenharmony_ci * Connect the phy specified @fwnode to the phylink instance specified
192962306a36Sopenharmony_ci * by @pl.
193062306a36Sopenharmony_ci *
193162306a36Sopenharmony_ci * Returns 0 on success or a negative errno.
193262306a36Sopenharmony_ci */
193362306a36Sopenharmony_ciint phylink_fwnode_phy_connect(struct phylink *pl,
193462306a36Sopenharmony_ci			       const struct fwnode_handle *fwnode,
193562306a36Sopenharmony_ci			       u32 flags)
193662306a36Sopenharmony_ci{
193762306a36Sopenharmony_ci	struct fwnode_handle *phy_fwnode;
193862306a36Sopenharmony_ci	struct phy_device *phy_dev;
193962306a36Sopenharmony_ci	int ret;
194062306a36Sopenharmony_ci
194162306a36Sopenharmony_ci	/* Fixed links and 802.3z are handled without needing a PHY */
194262306a36Sopenharmony_ci	if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
194362306a36Sopenharmony_ci	    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
194462306a36Sopenharmony_ci	     phy_interface_mode_is_8023z(pl->link_interface)))
194562306a36Sopenharmony_ci		return 0;
194662306a36Sopenharmony_ci
194762306a36Sopenharmony_ci	phy_fwnode = fwnode_get_phy_node(fwnode);
194862306a36Sopenharmony_ci	if (IS_ERR(phy_fwnode)) {
194962306a36Sopenharmony_ci		if (pl->cfg_link_an_mode == MLO_AN_PHY)
195062306a36Sopenharmony_ci			return -ENODEV;
195162306a36Sopenharmony_ci		return 0;
195262306a36Sopenharmony_ci	}
195362306a36Sopenharmony_ci
195462306a36Sopenharmony_ci	phy_dev = fwnode_phy_find_device(phy_fwnode);
195562306a36Sopenharmony_ci	/* We're done with the phy_node handle */
195662306a36Sopenharmony_ci	fwnode_handle_put(phy_fwnode);
195762306a36Sopenharmony_ci	if (!phy_dev)
195862306a36Sopenharmony_ci		return -ENODEV;
195962306a36Sopenharmony_ci
196062306a36Sopenharmony_ci	/* Use PHY device/driver interface */
196162306a36Sopenharmony_ci	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
196262306a36Sopenharmony_ci		pl->link_interface = phy_dev->interface;
196362306a36Sopenharmony_ci		pl->link_config.interface = pl->link_interface;
196462306a36Sopenharmony_ci	}
196562306a36Sopenharmony_ci
196662306a36Sopenharmony_ci	ret = phy_attach_direct(pl->netdev, phy_dev, flags,
196762306a36Sopenharmony_ci				pl->link_interface);
196862306a36Sopenharmony_ci	phy_device_free(phy_dev);
196962306a36Sopenharmony_ci	if (ret)
197062306a36Sopenharmony_ci		return ret;
197162306a36Sopenharmony_ci
197262306a36Sopenharmony_ci	ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
197362306a36Sopenharmony_ci	if (ret)
197462306a36Sopenharmony_ci		phy_detach(phy_dev);
197562306a36Sopenharmony_ci
197662306a36Sopenharmony_ci	return ret;
197762306a36Sopenharmony_ci}
197862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
197962306a36Sopenharmony_ci
198062306a36Sopenharmony_ci/**
198162306a36Sopenharmony_ci * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
198262306a36Sopenharmony_ci *   instance.
198362306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
198462306a36Sopenharmony_ci *
198562306a36Sopenharmony_ci * Disconnect any current PHY from the phylink instance described by @pl.
198662306a36Sopenharmony_ci */
198762306a36Sopenharmony_civoid phylink_disconnect_phy(struct phylink *pl)
198862306a36Sopenharmony_ci{
198962306a36Sopenharmony_ci	struct phy_device *phy;
199062306a36Sopenharmony_ci
199162306a36Sopenharmony_ci	ASSERT_RTNL();
199262306a36Sopenharmony_ci
199362306a36Sopenharmony_ci	phy = pl->phydev;
199462306a36Sopenharmony_ci	if (phy) {
199562306a36Sopenharmony_ci		mutex_lock(&phy->lock);
199662306a36Sopenharmony_ci		mutex_lock(&pl->state_mutex);
199762306a36Sopenharmony_ci		pl->phydev = NULL;
199862306a36Sopenharmony_ci		mutex_unlock(&pl->state_mutex);
199962306a36Sopenharmony_ci		mutex_unlock(&phy->lock);
200062306a36Sopenharmony_ci		flush_work(&pl->resolve);
200162306a36Sopenharmony_ci
200262306a36Sopenharmony_ci		phy_disconnect(phy);
200362306a36Sopenharmony_ci	}
200462306a36Sopenharmony_ci}
200562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_disconnect_phy);
200662306a36Sopenharmony_ci
200762306a36Sopenharmony_cistatic void phylink_link_changed(struct phylink *pl, bool up, const char *what)
200862306a36Sopenharmony_ci{
200962306a36Sopenharmony_ci	if (!up)
201062306a36Sopenharmony_ci		pl->mac_link_dropped = true;
201162306a36Sopenharmony_ci	phylink_run_resolve(pl);
201262306a36Sopenharmony_ci	phylink_dbg(pl, "%s link %s\n", what, up ? "up" : "down");
201362306a36Sopenharmony_ci}
201462306a36Sopenharmony_ci
201562306a36Sopenharmony_ci/**
201662306a36Sopenharmony_ci * phylink_mac_change() - notify phylink of a change in MAC state
201762306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
201862306a36Sopenharmony_ci * @up: indicates whether the link is currently up.
201962306a36Sopenharmony_ci *
202062306a36Sopenharmony_ci * The MAC driver should call this driver when the state of its link
202162306a36Sopenharmony_ci * changes (eg, link failure, new negotiation results, etc.)
202262306a36Sopenharmony_ci */
202362306a36Sopenharmony_civoid phylink_mac_change(struct phylink *pl, bool up)
202462306a36Sopenharmony_ci{
202562306a36Sopenharmony_ci	phylink_link_changed(pl, up, "mac");
202662306a36Sopenharmony_ci}
202762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_mac_change);
202862306a36Sopenharmony_ci
202962306a36Sopenharmony_ci/**
203062306a36Sopenharmony_ci * phylink_pcs_change() - notify phylink of a change to PCS link state
203162306a36Sopenharmony_ci * @pcs: pointer to &struct phylink_pcs
203262306a36Sopenharmony_ci * @up: indicates whether the link is currently up.
203362306a36Sopenharmony_ci *
203462306a36Sopenharmony_ci * The PCS driver should call this when the state of its link changes
203562306a36Sopenharmony_ci * (e.g. link failure, new negotiation results, etc.) Note: it should
203662306a36Sopenharmony_ci * not determine "up" by reading the BMSR. If in doubt about the link
203762306a36Sopenharmony_ci * state at interrupt time, then pass true if pcs_get_state() returns
203862306a36Sopenharmony_ci * the latched link-down state, otherwise pass false.
203962306a36Sopenharmony_ci */
204062306a36Sopenharmony_civoid phylink_pcs_change(struct phylink_pcs *pcs, bool up)
204162306a36Sopenharmony_ci{
204262306a36Sopenharmony_ci	struct phylink *pl = pcs->phylink;
204362306a36Sopenharmony_ci
204462306a36Sopenharmony_ci	if (pl)
204562306a36Sopenharmony_ci		phylink_link_changed(pl, up, "pcs");
204662306a36Sopenharmony_ci}
204762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_pcs_change);
204862306a36Sopenharmony_ci
204962306a36Sopenharmony_cistatic irqreturn_t phylink_link_handler(int irq, void *data)
205062306a36Sopenharmony_ci{
205162306a36Sopenharmony_ci	struct phylink *pl = data;
205262306a36Sopenharmony_ci
205362306a36Sopenharmony_ci	phylink_run_resolve(pl);
205462306a36Sopenharmony_ci
205562306a36Sopenharmony_ci	return IRQ_HANDLED;
205662306a36Sopenharmony_ci}
205762306a36Sopenharmony_ci
205862306a36Sopenharmony_ci/**
205962306a36Sopenharmony_ci * phylink_start() - start a phylink instance
206062306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
206162306a36Sopenharmony_ci *
206262306a36Sopenharmony_ci * Start the phylink instance specified by @pl, configuring the MAC for the
206362306a36Sopenharmony_ci * desired link mode(s) and negotiation style. This should be called from the
206462306a36Sopenharmony_ci * network device driver's &struct net_device_ops ndo_open() method.
206562306a36Sopenharmony_ci */
206662306a36Sopenharmony_civoid phylink_start(struct phylink *pl)
206762306a36Sopenharmony_ci{
206862306a36Sopenharmony_ci	bool poll = false;
206962306a36Sopenharmony_ci
207062306a36Sopenharmony_ci	ASSERT_RTNL();
207162306a36Sopenharmony_ci
207262306a36Sopenharmony_ci	phylink_info(pl, "configuring for %s/%s link mode\n",
207362306a36Sopenharmony_ci		     phylink_an_mode_str(pl->cur_link_an_mode),
207462306a36Sopenharmony_ci		     phy_modes(pl->link_config.interface));
207562306a36Sopenharmony_ci
207662306a36Sopenharmony_ci	/* Always set the carrier off */
207762306a36Sopenharmony_ci	if (pl->netdev)
207862306a36Sopenharmony_ci		netif_carrier_off(pl->netdev);
207962306a36Sopenharmony_ci
208062306a36Sopenharmony_ci	pl->pcs_state = PCS_STATE_STARTING;
208162306a36Sopenharmony_ci
208262306a36Sopenharmony_ci	/* Apply the link configuration to the MAC when starting. This allows
208362306a36Sopenharmony_ci	 * a fixed-link to start with the correct parameters, and also
208462306a36Sopenharmony_ci	 * ensures that we set the appropriate advertisement for Serdes links.
208562306a36Sopenharmony_ci	 *
208662306a36Sopenharmony_ci	 * Restart autonegotiation if using 802.3z to ensure that the link
208762306a36Sopenharmony_ci	 * parameters are properly negotiated.  This is necessary for DSA
208862306a36Sopenharmony_ci	 * switches using 802.3z negotiation to ensure they see our modes.
208962306a36Sopenharmony_ci	 */
209062306a36Sopenharmony_ci	phylink_mac_initial_config(pl, true);
209162306a36Sopenharmony_ci
209262306a36Sopenharmony_ci	pl->pcs_state = PCS_STATE_STARTED;
209362306a36Sopenharmony_ci
209462306a36Sopenharmony_ci	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
209562306a36Sopenharmony_ci
209662306a36Sopenharmony_ci	if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
209762306a36Sopenharmony_ci		int irq = gpiod_to_irq(pl->link_gpio);
209862306a36Sopenharmony_ci
209962306a36Sopenharmony_ci		if (irq > 0) {
210062306a36Sopenharmony_ci			if (!request_irq(irq, phylink_link_handler,
210162306a36Sopenharmony_ci					 IRQF_TRIGGER_RISING |
210262306a36Sopenharmony_ci					 IRQF_TRIGGER_FALLING,
210362306a36Sopenharmony_ci					 "netdev link", pl))
210462306a36Sopenharmony_ci				pl->link_irq = irq;
210562306a36Sopenharmony_ci			else
210662306a36Sopenharmony_ci				irq = 0;
210762306a36Sopenharmony_ci		}
210862306a36Sopenharmony_ci		if (irq <= 0)
210962306a36Sopenharmony_ci			poll = true;
211062306a36Sopenharmony_ci	}
211162306a36Sopenharmony_ci
211262306a36Sopenharmony_ci	if (pl->cfg_link_an_mode == MLO_AN_FIXED)
211362306a36Sopenharmony_ci		poll |= pl->config->poll_fixed_state;
211462306a36Sopenharmony_ci
211562306a36Sopenharmony_ci	if (poll)
211662306a36Sopenharmony_ci		mod_timer(&pl->link_poll, jiffies + HZ);
211762306a36Sopenharmony_ci	if (pl->phydev)
211862306a36Sopenharmony_ci		phy_start(pl->phydev);
211962306a36Sopenharmony_ci	if (pl->sfp_bus)
212062306a36Sopenharmony_ci		sfp_upstream_start(pl->sfp_bus);
212162306a36Sopenharmony_ci}
212262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_start);
212362306a36Sopenharmony_ci
212462306a36Sopenharmony_ci/**
212562306a36Sopenharmony_ci * phylink_stop() - stop a phylink instance
212662306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
212762306a36Sopenharmony_ci *
212862306a36Sopenharmony_ci * Stop the phylink instance specified by @pl. This should be called from the
212962306a36Sopenharmony_ci * network device driver's &struct net_device_ops ndo_stop() method.  The
213062306a36Sopenharmony_ci * network device's carrier state should not be changed prior to calling this
213162306a36Sopenharmony_ci * function.
213262306a36Sopenharmony_ci *
213362306a36Sopenharmony_ci * This will synchronously bring down the link if the link is not already
213462306a36Sopenharmony_ci * down (in other words, it will trigger a mac_link_down() method call.)
213562306a36Sopenharmony_ci */
213662306a36Sopenharmony_civoid phylink_stop(struct phylink *pl)
213762306a36Sopenharmony_ci{
213862306a36Sopenharmony_ci	ASSERT_RTNL();
213962306a36Sopenharmony_ci
214062306a36Sopenharmony_ci	if (pl->sfp_bus)
214162306a36Sopenharmony_ci		sfp_upstream_stop(pl->sfp_bus);
214262306a36Sopenharmony_ci	if (pl->phydev)
214362306a36Sopenharmony_ci		phy_stop(pl->phydev);
214462306a36Sopenharmony_ci	del_timer_sync(&pl->link_poll);
214562306a36Sopenharmony_ci	if (pl->link_irq) {
214662306a36Sopenharmony_ci		free_irq(pl->link_irq, pl);
214762306a36Sopenharmony_ci		pl->link_irq = 0;
214862306a36Sopenharmony_ci	}
214962306a36Sopenharmony_ci
215062306a36Sopenharmony_ci	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
215162306a36Sopenharmony_ci
215262306a36Sopenharmony_ci	pl->pcs_state = PCS_STATE_DOWN;
215362306a36Sopenharmony_ci
215462306a36Sopenharmony_ci	phylink_pcs_disable(pl->pcs);
215562306a36Sopenharmony_ci}
215662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_stop);
215762306a36Sopenharmony_ci
215862306a36Sopenharmony_ci/**
215962306a36Sopenharmony_ci * phylink_suspend() - handle a network device suspend event
216062306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
216162306a36Sopenharmony_ci * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
216262306a36Sopenharmony_ci *
216362306a36Sopenharmony_ci * Handle a network device suspend event. There are several cases:
216462306a36Sopenharmony_ci *
216562306a36Sopenharmony_ci * - If Wake-on-Lan is not active, we can bring down the link between
216662306a36Sopenharmony_ci *   the MAC and PHY by calling phylink_stop().
216762306a36Sopenharmony_ci * - If Wake-on-Lan is active, and being handled only by the PHY, we
216862306a36Sopenharmony_ci *   can also bring down the link between the MAC and PHY.
216962306a36Sopenharmony_ci * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
217062306a36Sopenharmony_ci *   still needs to receive packets, so we can not bring the link down.
217162306a36Sopenharmony_ci */
217262306a36Sopenharmony_civoid phylink_suspend(struct phylink *pl, bool mac_wol)
217362306a36Sopenharmony_ci{
217462306a36Sopenharmony_ci	ASSERT_RTNL();
217562306a36Sopenharmony_ci
217662306a36Sopenharmony_ci	if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) {
217762306a36Sopenharmony_ci		/* Wake-on-Lan enabled, MAC handling */
217862306a36Sopenharmony_ci		mutex_lock(&pl->state_mutex);
217962306a36Sopenharmony_ci
218062306a36Sopenharmony_ci		/* Stop the resolver bringing the link up */
218162306a36Sopenharmony_ci		__set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
218262306a36Sopenharmony_ci
218362306a36Sopenharmony_ci		/* Disable the carrier, to prevent transmit timeouts,
218462306a36Sopenharmony_ci		 * but one would hope all packets have been sent. This
218562306a36Sopenharmony_ci		 * also means phylink_resolve() will do nothing.
218662306a36Sopenharmony_ci		 */
218762306a36Sopenharmony_ci		if (pl->netdev)
218862306a36Sopenharmony_ci			netif_carrier_off(pl->netdev);
218962306a36Sopenharmony_ci		else
219062306a36Sopenharmony_ci			pl->old_link_state = false;
219162306a36Sopenharmony_ci
219262306a36Sopenharmony_ci		/* We do not call mac_link_down() here as we want the
219362306a36Sopenharmony_ci		 * link to remain up to receive the WoL packets.
219462306a36Sopenharmony_ci		 */
219562306a36Sopenharmony_ci		mutex_unlock(&pl->state_mutex);
219662306a36Sopenharmony_ci	} else {
219762306a36Sopenharmony_ci		phylink_stop(pl);
219862306a36Sopenharmony_ci	}
219962306a36Sopenharmony_ci}
220062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_suspend);
220162306a36Sopenharmony_ci
220262306a36Sopenharmony_ci/**
220362306a36Sopenharmony_ci * phylink_resume() - handle a network device resume event
220462306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
220562306a36Sopenharmony_ci *
220662306a36Sopenharmony_ci * Undo the effects of phylink_suspend(), returning the link to an
220762306a36Sopenharmony_ci * operational state.
220862306a36Sopenharmony_ci */
220962306a36Sopenharmony_civoid phylink_resume(struct phylink *pl)
221062306a36Sopenharmony_ci{
221162306a36Sopenharmony_ci	ASSERT_RTNL();
221262306a36Sopenharmony_ci
221362306a36Sopenharmony_ci	if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
221462306a36Sopenharmony_ci		/* Wake-on-Lan enabled, MAC handling */
221562306a36Sopenharmony_ci
221662306a36Sopenharmony_ci		/* Call mac_link_down() so we keep the overall state balanced.
221762306a36Sopenharmony_ci		 * Do this under the state_mutex lock for consistency. This
221862306a36Sopenharmony_ci		 * will cause a "Link Down" message to be printed during
221962306a36Sopenharmony_ci		 * resume, which is harmless - the true link state will be
222062306a36Sopenharmony_ci		 * printed when we run a resolve.
222162306a36Sopenharmony_ci		 */
222262306a36Sopenharmony_ci		mutex_lock(&pl->state_mutex);
222362306a36Sopenharmony_ci		phylink_link_down(pl);
222462306a36Sopenharmony_ci		mutex_unlock(&pl->state_mutex);
222562306a36Sopenharmony_ci
222662306a36Sopenharmony_ci		/* Re-apply the link parameters so that all the settings get
222762306a36Sopenharmony_ci		 * restored to the MAC.
222862306a36Sopenharmony_ci		 */
222962306a36Sopenharmony_ci		phylink_mac_initial_config(pl, true);
223062306a36Sopenharmony_ci
223162306a36Sopenharmony_ci		/* Re-enable and re-resolve the link parameters */
223262306a36Sopenharmony_ci		phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL);
223362306a36Sopenharmony_ci	} else {
223462306a36Sopenharmony_ci		phylink_start(pl);
223562306a36Sopenharmony_ci	}
223662306a36Sopenharmony_ci}
223762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_resume);
223862306a36Sopenharmony_ci
223962306a36Sopenharmony_ci/**
224062306a36Sopenharmony_ci * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
224162306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
224262306a36Sopenharmony_ci * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
224362306a36Sopenharmony_ci *
224462306a36Sopenharmony_ci * Read the wake on lan parameters from the PHY attached to the phylink
224562306a36Sopenharmony_ci * instance specified by @pl. If no PHY is currently attached, report no
224662306a36Sopenharmony_ci * support for wake on lan.
224762306a36Sopenharmony_ci */
224862306a36Sopenharmony_civoid phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
224962306a36Sopenharmony_ci{
225062306a36Sopenharmony_ci	ASSERT_RTNL();
225162306a36Sopenharmony_ci
225262306a36Sopenharmony_ci	wol->supported = 0;
225362306a36Sopenharmony_ci	wol->wolopts = 0;
225462306a36Sopenharmony_ci
225562306a36Sopenharmony_ci	if (pl->phydev)
225662306a36Sopenharmony_ci		phy_ethtool_get_wol(pl->phydev, wol);
225762306a36Sopenharmony_ci}
225862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
225962306a36Sopenharmony_ci
226062306a36Sopenharmony_ci/**
226162306a36Sopenharmony_ci * phylink_ethtool_set_wol() - set wake on lan parameters
226262306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
226362306a36Sopenharmony_ci * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
226462306a36Sopenharmony_ci *
226562306a36Sopenharmony_ci * Set the wake on lan parameters for the PHY attached to the phylink
226662306a36Sopenharmony_ci * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
226762306a36Sopenharmony_ci * error.
226862306a36Sopenharmony_ci *
226962306a36Sopenharmony_ci * Returns zero on success or negative errno code.
227062306a36Sopenharmony_ci */
227162306a36Sopenharmony_ciint phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
227262306a36Sopenharmony_ci{
227362306a36Sopenharmony_ci	int ret = -EOPNOTSUPP;
227462306a36Sopenharmony_ci
227562306a36Sopenharmony_ci	ASSERT_RTNL();
227662306a36Sopenharmony_ci
227762306a36Sopenharmony_ci	if (pl->phydev)
227862306a36Sopenharmony_ci		ret = phy_ethtool_set_wol(pl->phydev, wol);
227962306a36Sopenharmony_ci
228062306a36Sopenharmony_ci	return ret;
228162306a36Sopenharmony_ci}
228262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
228362306a36Sopenharmony_ci
228462306a36Sopenharmony_cistatic void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
228562306a36Sopenharmony_ci{
228662306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
228762306a36Sopenharmony_ci
228862306a36Sopenharmony_ci	linkmode_zero(mask);
228962306a36Sopenharmony_ci	phylink_set_port_modes(mask);
229062306a36Sopenharmony_ci
229162306a36Sopenharmony_ci	linkmode_and(dst, dst, mask);
229262306a36Sopenharmony_ci	linkmode_or(dst, dst, b);
229362306a36Sopenharmony_ci}
229462306a36Sopenharmony_ci
229562306a36Sopenharmony_cistatic void phylink_get_ksettings(const struct phylink_link_state *state,
229662306a36Sopenharmony_ci				  struct ethtool_link_ksettings *kset)
229762306a36Sopenharmony_ci{
229862306a36Sopenharmony_ci	phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
229962306a36Sopenharmony_ci	linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
230062306a36Sopenharmony_ci	if (kset->base.rate_matching == RATE_MATCH_NONE) {
230162306a36Sopenharmony_ci		kset->base.speed = state->speed;
230262306a36Sopenharmony_ci		kset->base.duplex = state->duplex;
230362306a36Sopenharmony_ci	}
230462306a36Sopenharmony_ci	kset->base.autoneg = linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
230562306a36Sopenharmony_ci					       state->advertising) ?
230662306a36Sopenharmony_ci				AUTONEG_ENABLE : AUTONEG_DISABLE;
230762306a36Sopenharmony_ci}
230862306a36Sopenharmony_ci
230962306a36Sopenharmony_ci/**
231062306a36Sopenharmony_ci * phylink_ethtool_ksettings_get() - get the current link settings
231162306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
231262306a36Sopenharmony_ci * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
231362306a36Sopenharmony_ci *
231462306a36Sopenharmony_ci * Read the current link settings for the phylink instance specified by @pl.
231562306a36Sopenharmony_ci * This will be the link settings read from the MAC, PHY or fixed link
231662306a36Sopenharmony_ci * settings depending on the current negotiation mode.
231762306a36Sopenharmony_ci */
231862306a36Sopenharmony_ciint phylink_ethtool_ksettings_get(struct phylink *pl,
231962306a36Sopenharmony_ci				  struct ethtool_link_ksettings *kset)
232062306a36Sopenharmony_ci{
232162306a36Sopenharmony_ci	struct phylink_link_state link_state;
232262306a36Sopenharmony_ci
232362306a36Sopenharmony_ci	ASSERT_RTNL();
232462306a36Sopenharmony_ci
232562306a36Sopenharmony_ci	if (pl->phydev)
232662306a36Sopenharmony_ci		phy_ethtool_ksettings_get(pl->phydev, kset);
232762306a36Sopenharmony_ci	else
232862306a36Sopenharmony_ci		kset->base.port = pl->link_port;
232962306a36Sopenharmony_ci
233062306a36Sopenharmony_ci	linkmode_copy(kset->link_modes.supported, pl->supported);
233162306a36Sopenharmony_ci
233262306a36Sopenharmony_ci	switch (pl->cur_link_an_mode) {
233362306a36Sopenharmony_ci	case MLO_AN_FIXED:
233462306a36Sopenharmony_ci		/* We are using fixed settings. Report these as the
233562306a36Sopenharmony_ci		 * current link settings - and note that these also
233662306a36Sopenharmony_ci		 * represent the supported speeds/duplex/pause modes.
233762306a36Sopenharmony_ci		 */
233862306a36Sopenharmony_ci		phylink_get_fixed_state(pl, &link_state);
233962306a36Sopenharmony_ci		phylink_get_ksettings(&link_state, kset);
234062306a36Sopenharmony_ci		break;
234162306a36Sopenharmony_ci
234262306a36Sopenharmony_ci	case MLO_AN_INBAND:
234362306a36Sopenharmony_ci		/* If there is a phy attached, then use the reported
234462306a36Sopenharmony_ci		 * settings from the phy with no modification.
234562306a36Sopenharmony_ci		 */
234662306a36Sopenharmony_ci		if (pl->phydev)
234762306a36Sopenharmony_ci			break;
234862306a36Sopenharmony_ci
234962306a36Sopenharmony_ci		phylink_mac_pcs_get_state(pl, &link_state);
235062306a36Sopenharmony_ci
235162306a36Sopenharmony_ci		/* The MAC is reporting the link results from its own PCS
235262306a36Sopenharmony_ci		 * layer via in-band status. Report these as the current
235362306a36Sopenharmony_ci		 * link settings.
235462306a36Sopenharmony_ci		 */
235562306a36Sopenharmony_ci		phylink_get_ksettings(&link_state, kset);
235662306a36Sopenharmony_ci		break;
235762306a36Sopenharmony_ci	}
235862306a36Sopenharmony_ci
235962306a36Sopenharmony_ci	return 0;
236062306a36Sopenharmony_ci}
236162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
236262306a36Sopenharmony_ci
236362306a36Sopenharmony_ci/**
236462306a36Sopenharmony_ci * phylink_ethtool_ksettings_set() - set the link settings
236562306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
236662306a36Sopenharmony_ci * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
236762306a36Sopenharmony_ci */
236862306a36Sopenharmony_ciint phylink_ethtool_ksettings_set(struct phylink *pl,
236962306a36Sopenharmony_ci				  const struct ethtool_link_ksettings *kset)
237062306a36Sopenharmony_ci{
237162306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
237262306a36Sopenharmony_ci	struct phylink_link_state config;
237362306a36Sopenharmony_ci	const struct phy_setting *s;
237462306a36Sopenharmony_ci
237562306a36Sopenharmony_ci	ASSERT_RTNL();
237662306a36Sopenharmony_ci
237762306a36Sopenharmony_ci	if (pl->phydev) {
237862306a36Sopenharmony_ci		struct ethtool_link_ksettings phy_kset = *kset;
237962306a36Sopenharmony_ci
238062306a36Sopenharmony_ci		linkmode_and(phy_kset.link_modes.advertising,
238162306a36Sopenharmony_ci			     phy_kset.link_modes.advertising,
238262306a36Sopenharmony_ci			     pl->supported);
238362306a36Sopenharmony_ci
238462306a36Sopenharmony_ci		/* We can rely on phylib for this update; we also do not need
238562306a36Sopenharmony_ci		 * to update the pl->link_config settings:
238662306a36Sopenharmony_ci		 * - the configuration returned via ksettings_get() will come
238762306a36Sopenharmony_ci		 *   from phylib whenever a PHY is present.
238862306a36Sopenharmony_ci		 * - link_config.interface will be updated by the PHY calling
238962306a36Sopenharmony_ci		 *   back via phylink_phy_change() and a subsequent resolve.
239062306a36Sopenharmony_ci		 * - initial link configuration for PHY mode comes from the
239162306a36Sopenharmony_ci		 *   last phy state updated via phylink_phy_change().
239262306a36Sopenharmony_ci		 * - other configuration changes (e.g. pause modes) are
239362306a36Sopenharmony_ci		 *   performed directly via phylib.
239462306a36Sopenharmony_ci		 * - if in in-band mode with a PHY, the link configuration
239562306a36Sopenharmony_ci		 *   is passed on the link from the PHY, and all of
239662306a36Sopenharmony_ci		 *   link_config.{speed,duplex,an_enabled,pause} are not used.
239762306a36Sopenharmony_ci		 * - the only possible use would be link_config.advertising
239862306a36Sopenharmony_ci		 *   pause modes when in 1000base-X mode with a PHY, but in
239962306a36Sopenharmony_ci		 *   the presence of a PHY, this should not be changed as that
240062306a36Sopenharmony_ci		 *   should be determined from the media side advertisement.
240162306a36Sopenharmony_ci		 */
240262306a36Sopenharmony_ci		return phy_ethtool_ksettings_set(pl->phydev, &phy_kset);
240362306a36Sopenharmony_ci	}
240462306a36Sopenharmony_ci
240562306a36Sopenharmony_ci	config = pl->link_config;
240662306a36Sopenharmony_ci	/* Mask out unsupported advertisements */
240762306a36Sopenharmony_ci	linkmode_and(config.advertising, kset->link_modes.advertising,
240862306a36Sopenharmony_ci		     pl->supported);
240962306a36Sopenharmony_ci
241062306a36Sopenharmony_ci	/* FIXME: should we reject autoneg if phy/mac does not support it? */
241162306a36Sopenharmony_ci	switch (kset->base.autoneg) {
241262306a36Sopenharmony_ci	case AUTONEG_DISABLE:
241362306a36Sopenharmony_ci		/* Autonegotiation disabled, select a suitable speed and
241462306a36Sopenharmony_ci		 * duplex.
241562306a36Sopenharmony_ci		 */
241662306a36Sopenharmony_ci		s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
241762306a36Sopenharmony_ci				       pl->supported, false);
241862306a36Sopenharmony_ci		if (!s)
241962306a36Sopenharmony_ci			return -EINVAL;
242062306a36Sopenharmony_ci
242162306a36Sopenharmony_ci		/* If we have a fixed link, refuse to change link parameters.
242262306a36Sopenharmony_ci		 * If the link parameters match, accept them but do nothing.
242362306a36Sopenharmony_ci		 */
242462306a36Sopenharmony_ci		if (pl->cur_link_an_mode == MLO_AN_FIXED) {
242562306a36Sopenharmony_ci			if (s->speed != pl->link_config.speed ||
242662306a36Sopenharmony_ci			    s->duplex != pl->link_config.duplex)
242762306a36Sopenharmony_ci				return -EINVAL;
242862306a36Sopenharmony_ci			return 0;
242962306a36Sopenharmony_ci		}
243062306a36Sopenharmony_ci
243162306a36Sopenharmony_ci		config.speed = s->speed;
243262306a36Sopenharmony_ci		config.duplex = s->duplex;
243362306a36Sopenharmony_ci		break;
243462306a36Sopenharmony_ci
243562306a36Sopenharmony_ci	case AUTONEG_ENABLE:
243662306a36Sopenharmony_ci		/* If we have a fixed link, allow autonegotiation (since that
243762306a36Sopenharmony_ci		 * is our default case) but do not allow the advertisement to
243862306a36Sopenharmony_ci		 * be changed. If the advertisement matches, simply return.
243962306a36Sopenharmony_ci		 */
244062306a36Sopenharmony_ci		if (pl->cur_link_an_mode == MLO_AN_FIXED) {
244162306a36Sopenharmony_ci			if (!linkmode_equal(config.advertising,
244262306a36Sopenharmony_ci					    pl->link_config.advertising))
244362306a36Sopenharmony_ci				return -EINVAL;
244462306a36Sopenharmony_ci			return 0;
244562306a36Sopenharmony_ci		}
244662306a36Sopenharmony_ci
244762306a36Sopenharmony_ci		config.speed = SPEED_UNKNOWN;
244862306a36Sopenharmony_ci		config.duplex = DUPLEX_UNKNOWN;
244962306a36Sopenharmony_ci		break;
245062306a36Sopenharmony_ci
245162306a36Sopenharmony_ci	default:
245262306a36Sopenharmony_ci		return -EINVAL;
245362306a36Sopenharmony_ci	}
245462306a36Sopenharmony_ci
245562306a36Sopenharmony_ci	/* We have ruled out the case with a PHY attached, and the
245662306a36Sopenharmony_ci	 * fixed-link cases.  All that is left are in-band links.
245762306a36Sopenharmony_ci	 */
245862306a36Sopenharmony_ci	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
245962306a36Sopenharmony_ci			 kset->base.autoneg == AUTONEG_ENABLE);
246062306a36Sopenharmony_ci
246162306a36Sopenharmony_ci	/* If this link is with an SFP, ensure that changes to advertised modes
246262306a36Sopenharmony_ci	 * also cause the associated interface to be selected such that the
246362306a36Sopenharmony_ci	 * link can be configured correctly.
246462306a36Sopenharmony_ci	 */
246562306a36Sopenharmony_ci	if (pl->sfp_bus) {
246662306a36Sopenharmony_ci		config.interface = sfp_select_interface(pl->sfp_bus,
246762306a36Sopenharmony_ci							config.advertising);
246862306a36Sopenharmony_ci		if (config.interface == PHY_INTERFACE_MODE_NA) {
246962306a36Sopenharmony_ci			phylink_err(pl,
247062306a36Sopenharmony_ci				    "selection of interface failed, advertisement %*pb\n",
247162306a36Sopenharmony_ci				    __ETHTOOL_LINK_MODE_MASK_NBITS,
247262306a36Sopenharmony_ci				    config.advertising);
247362306a36Sopenharmony_ci			return -EINVAL;
247462306a36Sopenharmony_ci		}
247562306a36Sopenharmony_ci
247662306a36Sopenharmony_ci		/* Revalidate with the selected interface */
247762306a36Sopenharmony_ci		linkmode_copy(support, pl->supported);
247862306a36Sopenharmony_ci		if (phylink_validate(pl, support, &config)) {
247962306a36Sopenharmony_ci			phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
248062306a36Sopenharmony_ci				    phylink_an_mode_str(pl->cur_link_an_mode),
248162306a36Sopenharmony_ci				    phy_modes(config.interface),
248262306a36Sopenharmony_ci				    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
248362306a36Sopenharmony_ci			return -EINVAL;
248462306a36Sopenharmony_ci		}
248562306a36Sopenharmony_ci	} else {
248662306a36Sopenharmony_ci		/* Validate without changing the current supported mask. */
248762306a36Sopenharmony_ci		linkmode_copy(support, pl->supported);
248862306a36Sopenharmony_ci		if (phylink_validate(pl, support, &config))
248962306a36Sopenharmony_ci			return -EINVAL;
249062306a36Sopenharmony_ci	}
249162306a36Sopenharmony_ci
249262306a36Sopenharmony_ci	/* If autonegotiation is enabled, we must have an advertisement */
249362306a36Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
249462306a36Sopenharmony_ci			      config.advertising) &&
249562306a36Sopenharmony_ci	    phylink_is_empty_linkmode(config.advertising))
249662306a36Sopenharmony_ci		return -EINVAL;
249762306a36Sopenharmony_ci
249862306a36Sopenharmony_ci	mutex_lock(&pl->state_mutex);
249962306a36Sopenharmony_ci	pl->link_config.speed = config.speed;
250062306a36Sopenharmony_ci	pl->link_config.duplex = config.duplex;
250162306a36Sopenharmony_ci
250262306a36Sopenharmony_ci	if (pl->link_config.interface != config.interface) {
250362306a36Sopenharmony_ci		/* The interface changed, e.g. 1000base-X <-> 2500base-X */
250462306a36Sopenharmony_ci		/* We need to force the link down, then change the interface */
250562306a36Sopenharmony_ci		if (pl->old_link_state) {
250662306a36Sopenharmony_ci			phylink_link_down(pl);
250762306a36Sopenharmony_ci			pl->old_link_state = false;
250862306a36Sopenharmony_ci		}
250962306a36Sopenharmony_ci		if (!test_bit(PHYLINK_DISABLE_STOPPED,
251062306a36Sopenharmony_ci			      &pl->phylink_disable_state))
251162306a36Sopenharmony_ci			phylink_major_config(pl, false, &config);
251262306a36Sopenharmony_ci		pl->link_config.interface = config.interface;
251362306a36Sopenharmony_ci		linkmode_copy(pl->link_config.advertising, config.advertising);
251462306a36Sopenharmony_ci	} else if (!linkmode_equal(pl->link_config.advertising,
251562306a36Sopenharmony_ci				   config.advertising)) {
251662306a36Sopenharmony_ci		linkmode_copy(pl->link_config.advertising, config.advertising);
251762306a36Sopenharmony_ci		phylink_change_inband_advert(pl);
251862306a36Sopenharmony_ci	}
251962306a36Sopenharmony_ci	mutex_unlock(&pl->state_mutex);
252062306a36Sopenharmony_ci
252162306a36Sopenharmony_ci	return 0;
252262306a36Sopenharmony_ci}
252362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
252462306a36Sopenharmony_ci
252562306a36Sopenharmony_ci/**
252662306a36Sopenharmony_ci * phylink_ethtool_nway_reset() - restart negotiation
252762306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
252862306a36Sopenharmony_ci *
252962306a36Sopenharmony_ci * Restart negotiation for the phylink instance specified by @pl. This will
253062306a36Sopenharmony_ci * cause any attached phy to restart negotiation with the link partner, and
253162306a36Sopenharmony_ci * if the MAC is in a BaseX mode, the MAC will also be requested to restart
253262306a36Sopenharmony_ci * negotiation.
253362306a36Sopenharmony_ci *
253462306a36Sopenharmony_ci * Returns zero on success, or negative error code.
253562306a36Sopenharmony_ci */
253662306a36Sopenharmony_ciint phylink_ethtool_nway_reset(struct phylink *pl)
253762306a36Sopenharmony_ci{
253862306a36Sopenharmony_ci	int ret = 0;
253962306a36Sopenharmony_ci
254062306a36Sopenharmony_ci	ASSERT_RTNL();
254162306a36Sopenharmony_ci
254262306a36Sopenharmony_ci	if (pl->phydev)
254362306a36Sopenharmony_ci		ret = phy_restart_aneg(pl->phydev);
254462306a36Sopenharmony_ci	phylink_pcs_an_restart(pl);
254562306a36Sopenharmony_ci
254662306a36Sopenharmony_ci	return ret;
254762306a36Sopenharmony_ci}
254862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
254962306a36Sopenharmony_ci
255062306a36Sopenharmony_ci/**
255162306a36Sopenharmony_ci * phylink_ethtool_get_pauseparam() - get the current pause parameters
255262306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
255362306a36Sopenharmony_ci * @pause: a pointer to a &struct ethtool_pauseparam
255462306a36Sopenharmony_ci */
255562306a36Sopenharmony_civoid phylink_ethtool_get_pauseparam(struct phylink *pl,
255662306a36Sopenharmony_ci				    struct ethtool_pauseparam *pause)
255762306a36Sopenharmony_ci{
255862306a36Sopenharmony_ci	ASSERT_RTNL();
255962306a36Sopenharmony_ci
256062306a36Sopenharmony_ci	pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
256162306a36Sopenharmony_ci	pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
256262306a36Sopenharmony_ci	pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
256362306a36Sopenharmony_ci}
256462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
256562306a36Sopenharmony_ci
256662306a36Sopenharmony_ci/**
256762306a36Sopenharmony_ci * phylink_ethtool_set_pauseparam() - set the current pause parameters
256862306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
256962306a36Sopenharmony_ci * @pause: a pointer to a &struct ethtool_pauseparam
257062306a36Sopenharmony_ci */
257162306a36Sopenharmony_ciint phylink_ethtool_set_pauseparam(struct phylink *pl,
257262306a36Sopenharmony_ci				   struct ethtool_pauseparam *pause)
257362306a36Sopenharmony_ci{
257462306a36Sopenharmony_ci	struct phylink_link_state *config = &pl->link_config;
257562306a36Sopenharmony_ci	bool manual_changed;
257662306a36Sopenharmony_ci	int pause_state;
257762306a36Sopenharmony_ci
257862306a36Sopenharmony_ci	ASSERT_RTNL();
257962306a36Sopenharmony_ci
258062306a36Sopenharmony_ci	if (pl->cur_link_an_mode == MLO_AN_FIXED)
258162306a36Sopenharmony_ci		return -EOPNOTSUPP;
258262306a36Sopenharmony_ci
258362306a36Sopenharmony_ci	if (!phylink_test(pl->supported, Pause) &&
258462306a36Sopenharmony_ci	    !phylink_test(pl->supported, Asym_Pause))
258562306a36Sopenharmony_ci		return -EOPNOTSUPP;
258662306a36Sopenharmony_ci
258762306a36Sopenharmony_ci	if (!phylink_test(pl->supported, Asym_Pause) &&
258862306a36Sopenharmony_ci	    pause->rx_pause != pause->tx_pause)
258962306a36Sopenharmony_ci		return -EINVAL;
259062306a36Sopenharmony_ci
259162306a36Sopenharmony_ci	pause_state = 0;
259262306a36Sopenharmony_ci	if (pause->autoneg)
259362306a36Sopenharmony_ci		pause_state |= MLO_PAUSE_AN;
259462306a36Sopenharmony_ci	if (pause->rx_pause)
259562306a36Sopenharmony_ci		pause_state |= MLO_PAUSE_RX;
259662306a36Sopenharmony_ci	if (pause->tx_pause)
259762306a36Sopenharmony_ci		pause_state |= MLO_PAUSE_TX;
259862306a36Sopenharmony_ci
259962306a36Sopenharmony_ci	mutex_lock(&pl->state_mutex);
260062306a36Sopenharmony_ci	/*
260162306a36Sopenharmony_ci	 * See the comments for linkmode_set_pause(), wrt the deficiencies
260262306a36Sopenharmony_ci	 * with the current implementation.  A solution to this issue would
260362306a36Sopenharmony_ci	 * be:
260462306a36Sopenharmony_ci	 * ethtool  Local device
260562306a36Sopenharmony_ci	 *  rx  tx  Pause AsymDir
260662306a36Sopenharmony_ci	 *  0   0   0     0
260762306a36Sopenharmony_ci	 *  1   0   1     1
260862306a36Sopenharmony_ci	 *  0   1   0     1
260962306a36Sopenharmony_ci	 *  1   1   1     1
261062306a36Sopenharmony_ci	 * and then use the ethtool rx/tx enablement status to mask the
261162306a36Sopenharmony_ci	 * rx/tx pause resolution.
261262306a36Sopenharmony_ci	 */
261362306a36Sopenharmony_ci	linkmode_set_pause(config->advertising, pause->tx_pause,
261462306a36Sopenharmony_ci			   pause->rx_pause);
261562306a36Sopenharmony_ci
261662306a36Sopenharmony_ci	manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
261762306a36Sopenharmony_ci			 (!(pause_state & MLO_PAUSE_AN) &&
261862306a36Sopenharmony_ci			   (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
261962306a36Sopenharmony_ci
262062306a36Sopenharmony_ci	config->pause = pause_state;
262162306a36Sopenharmony_ci
262262306a36Sopenharmony_ci	/* Update our in-band advertisement, triggering a renegotiation if
262362306a36Sopenharmony_ci	 * the advertisement changed.
262462306a36Sopenharmony_ci	 */
262562306a36Sopenharmony_ci	if (!pl->phydev)
262662306a36Sopenharmony_ci		phylink_change_inband_advert(pl);
262762306a36Sopenharmony_ci
262862306a36Sopenharmony_ci	mutex_unlock(&pl->state_mutex);
262962306a36Sopenharmony_ci
263062306a36Sopenharmony_ci	/* If we have a PHY, a change of the pause frame advertisement will
263162306a36Sopenharmony_ci	 * cause phylib to renegotiate (if AN is enabled) which will in turn
263262306a36Sopenharmony_ci	 * call our phylink_phy_change() and trigger a resolve.  Note that
263362306a36Sopenharmony_ci	 * we can't hold our state mutex while calling phy_set_asym_pause().
263462306a36Sopenharmony_ci	 */
263562306a36Sopenharmony_ci	if (pl->phydev)
263662306a36Sopenharmony_ci		phy_set_asym_pause(pl->phydev, pause->rx_pause,
263762306a36Sopenharmony_ci				   pause->tx_pause);
263862306a36Sopenharmony_ci
263962306a36Sopenharmony_ci	/* If the manual pause settings changed, make sure we trigger a
264062306a36Sopenharmony_ci	 * resolve to update their state; we can not guarantee that the
264162306a36Sopenharmony_ci	 * link will cycle.
264262306a36Sopenharmony_ci	 */
264362306a36Sopenharmony_ci	if (manual_changed) {
264462306a36Sopenharmony_ci		pl->mac_link_dropped = true;
264562306a36Sopenharmony_ci		phylink_run_resolve(pl);
264662306a36Sopenharmony_ci	}
264762306a36Sopenharmony_ci
264862306a36Sopenharmony_ci	return 0;
264962306a36Sopenharmony_ci}
265062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
265162306a36Sopenharmony_ci
265262306a36Sopenharmony_ci/**
265362306a36Sopenharmony_ci * phylink_get_eee_err() - read the energy efficient ethernet error
265462306a36Sopenharmony_ci *   counter
265562306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create().
265662306a36Sopenharmony_ci *
265762306a36Sopenharmony_ci * Read the Energy Efficient Ethernet error counter from the PHY associated
265862306a36Sopenharmony_ci * with the phylink instance specified by @pl.
265962306a36Sopenharmony_ci *
266062306a36Sopenharmony_ci * Returns positive error counter value, or negative error code.
266162306a36Sopenharmony_ci */
266262306a36Sopenharmony_ciint phylink_get_eee_err(struct phylink *pl)
266362306a36Sopenharmony_ci{
266462306a36Sopenharmony_ci	int ret = 0;
266562306a36Sopenharmony_ci
266662306a36Sopenharmony_ci	ASSERT_RTNL();
266762306a36Sopenharmony_ci
266862306a36Sopenharmony_ci	if (pl->phydev)
266962306a36Sopenharmony_ci		ret = phy_get_eee_err(pl->phydev);
267062306a36Sopenharmony_ci
267162306a36Sopenharmony_ci	return ret;
267262306a36Sopenharmony_ci}
267362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_get_eee_err);
267462306a36Sopenharmony_ci
267562306a36Sopenharmony_ci/**
267662306a36Sopenharmony_ci * phylink_init_eee() - init and check the EEE features
267762306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
267862306a36Sopenharmony_ci * @clk_stop_enable: allow PHY to stop receive clock
267962306a36Sopenharmony_ci *
268062306a36Sopenharmony_ci * Must be called either with RTNL held or within mac_link_up()
268162306a36Sopenharmony_ci */
268262306a36Sopenharmony_ciint phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
268362306a36Sopenharmony_ci{
268462306a36Sopenharmony_ci	int ret = -EOPNOTSUPP;
268562306a36Sopenharmony_ci
268662306a36Sopenharmony_ci	if (pl->phydev)
268762306a36Sopenharmony_ci		ret = phy_init_eee(pl->phydev, clk_stop_enable);
268862306a36Sopenharmony_ci
268962306a36Sopenharmony_ci	return ret;
269062306a36Sopenharmony_ci}
269162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_init_eee);
269262306a36Sopenharmony_ci
269362306a36Sopenharmony_ci/**
269462306a36Sopenharmony_ci * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
269562306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
269662306a36Sopenharmony_ci * @eee: a pointer to a &struct ethtool_eee for the read parameters
269762306a36Sopenharmony_ci */
269862306a36Sopenharmony_ciint phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
269962306a36Sopenharmony_ci{
270062306a36Sopenharmony_ci	int ret = -EOPNOTSUPP;
270162306a36Sopenharmony_ci
270262306a36Sopenharmony_ci	ASSERT_RTNL();
270362306a36Sopenharmony_ci
270462306a36Sopenharmony_ci	if (pl->phydev)
270562306a36Sopenharmony_ci		ret = phy_ethtool_get_eee(pl->phydev, eee);
270662306a36Sopenharmony_ci
270762306a36Sopenharmony_ci	return ret;
270862306a36Sopenharmony_ci}
270962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
271062306a36Sopenharmony_ci
271162306a36Sopenharmony_ci/**
271262306a36Sopenharmony_ci * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
271362306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
271462306a36Sopenharmony_ci * @eee: a pointer to a &struct ethtool_eee for the desired parameters
271562306a36Sopenharmony_ci */
271662306a36Sopenharmony_ciint phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
271762306a36Sopenharmony_ci{
271862306a36Sopenharmony_ci	int ret = -EOPNOTSUPP;
271962306a36Sopenharmony_ci
272062306a36Sopenharmony_ci	ASSERT_RTNL();
272162306a36Sopenharmony_ci
272262306a36Sopenharmony_ci	if (pl->phydev)
272362306a36Sopenharmony_ci		ret = phy_ethtool_set_eee(pl->phydev, eee);
272462306a36Sopenharmony_ci
272562306a36Sopenharmony_ci	return ret;
272662306a36Sopenharmony_ci}
272762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
272862306a36Sopenharmony_ci
272962306a36Sopenharmony_ci/* This emulates MII registers for a fixed-mode phy operating as per the
273062306a36Sopenharmony_ci * passed in state. "aneg" defines if we report negotiation is possible.
273162306a36Sopenharmony_ci *
273262306a36Sopenharmony_ci * FIXME: should deal with negotiation state too.
273362306a36Sopenharmony_ci */
273462306a36Sopenharmony_cistatic int phylink_mii_emul_read(unsigned int reg,
273562306a36Sopenharmony_ci				 struct phylink_link_state *state)
273662306a36Sopenharmony_ci{
273762306a36Sopenharmony_ci	struct fixed_phy_status fs;
273862306a36Sopenharmony_ci	unsigned long *lpa = state->lp_advertising;
273962306a36Sopenharmony_ci	int val;
274062306a36Sopenharmony_ci
274162306a36Sopenharmony_ci	fs.link = state->link;
274262306a36Sopenharmony_ci	fs.speed = state->speed;
274362306a36Sopenharmony_ci	fs.duplex = state->duplex;
274462306a36Sopenharmony_ci	fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
274562306a36Sopenharmony_ci	fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
274662306a36Sopenharmony_ci
274762306a36Sopenharmony_ci	val = swphy_read_reg(reg, &fs);
274862306a36Sopenharmony_ci	if (reg == MII_BMSR) {
274962306a36Sopenharmony_ci		if (!state->an_complete)
275062306a36Sopenharmony_ci			val &= ~BMSR_ANEGCOMPLETE;
275162306a36Sopenharmony_ci	}
275262306a36Sopenharmony_ci	return val;
275362306a36Sopenharmony_ci}
275462306a36Sopenharmony_ci
275562306a36Sopenharmony_cistatic int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
275662306a36Sopenharmony_ci			    unsigned int reg)
275762306a36Sopenharmony_ci{
275862306a36Sopenharmony_ci	struct phy_device *phydev = pl->phydev;
275962306a36Sopenharmony_ci	int prtad, devad;
276062306a36Sopenharmony_ci
276162306a36Sopenharmony_ci	if (mdio_phy_id_is_c45(phy_id)) {
276262306a36Sopenharmony_ci		prtad = mdio_phy_id_prtad(phy_id);
276362306a36Sopenharmony_ci		devad = mdio_phy_id_devad(phy_id);
276462306a36Sopenharmony_ci		return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
276562306a36Sopenharmony_ci					reg);
276662306a36Sopenharmony_ci	}
276762306a36Sopenharmony_ci
276862306a36Sopenharmony_ci	if (phydev->is_c45) {
276962306a36Sopenharmony_ci		switch (reg) {
277062306a36Sopenharmony_ci		case MII_BMCR:
277162306a36Sopenharmony_ci		case MII_BMSR:
277262306a36Sopenharmony_ci		case MII_PHYSID1:
277362306a36Sopenharmony_ci		case MII_PHYSID2:
277462306a36Sopenharmony_ci			devad = __ffs(phydev->c45_ids.mmds_present);
277562306a36Sopenharmony_ci			break;
277662306a36Sopenharmony_ci		case MII_ADVERTISE:
277762306a36Sopenharmony_ci		case MII_LPA:
277862306a36Sopenharmony_ci			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
277962306a36Sopenharmony_ci				return -EINVAL;
278062306a36Sopenharmony_ci			devad = MDIO_MMD_AN;
278162306a36Sopenharmony_ci			if (reg == MII_ADVERTISE)
278262306a36Sopenharmony_ci				reg = MDIO_AN_ADVERTISE;
278362306a36Sopenharmony_ci			else
278462306a36Sopenharmony_ci				reg = MDIO_AN_LPA;
278562306a36Sopenharmony_ci			break;
278662306a36Sopenharmony_ci		default:
278762306a36Sopenharmony_ci			return -EINVAL;
278862306a36Sopenharmony_ci		}
278962306a36Sopenharmony_ci		prtad = phy_id;
279062306a36Sopenharmony_ci		return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
279162306a36Sopenharmony_ci					reg);
279262306a36Sopenharmony_ci	}
279362306a36Sopenharmony_ci
279462306a36Sopenharmony_ci	return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg);
279562306a36Sopenharmony_ci}
279662306a36Sopenharmony_ci
279762306a36Sopenharmony_cistatic int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
279862306a36Sopenharmony_ci			     unsigned int reg, unsigned int val)
279962306a36Sopenharmony_ci{
280062306a36Sopenharmony_ci	struct phy_device *phydev = pl->phydev;
280162306a36Sopenharmony_ci	int prtad, devad;
280262306a36Sopenharmony_ci
280362306a36Sopenharmony_ci	if (mdio_phy_id_is_c45(phy_id)) {
280462306a36Sopenharmony_ci		prtad = mdio_phy_id_prtad(phy_id);
280562306a36Sopenharmony_ci		devad = mdio_phy_id_devad(phy_id);
280662306a36Sopenharmony_ci		return mdiobus_c45_write(pl->phydev->mdio.bus, prtad, devad,
280762306a36Sopenharmony_ci					 reg, val);
280862306a36Sopenharmony_ci	}
280962306a36Sopenharmony_ci
281062306a36Sopenharmony_ci	if (phydev->is_c45) {
281162306a36Sopenharmony_ci		switch (reg) {
281262306a36Sopenharmony_ci		case MII_BMCR:
281362306a36Sopenharmony_ci		case MII_BMSR:
281462306a36Sopenharmony_ci		case MII_PHYSID1:
281562306a36Sopenharmony_ci		case MII_PHYSID2:
281662306a36Sopenharmony_ci			devad = __ffs(phydev->c45_ids.mmds_present);
281762306a36Sopenharmony_ci			break;
281862306a36Sopenharmony_ci		case MII_ADVERTISE:
281962306a36Sopenharmony_ci		case MII_LPA:
282062306a36Sopenharmony_ci			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
282162306a36Sopenharmony_ci				return -EINVAL;
282262306a36Sopenharmony_ci			devad = MDIO_MMD_AN;
282362306a36Sopenharmony_ci			if (reg == MII_ADVERTISE)
282462306a36Sopenharmony_ci				reg = MDIO_AN_ADVERTISE;
282562306a36Sopenharmony_ci			else
282662306a36Sopenharmony_ci				reg = MDIO_AN_LPA;
282762306a36Sopenharmony_ci			break;
282862306a36Sopenharmony_ci		default:
282962306a36Sopenharmony_ci			return -EINVAL;
283062306a36Sopenharmony_ci		}
283162306a36Sopenharmony_ci		return mdiobus_c45_write(pl->phydev->mdio.bus, phy_id, devad,
283262306a36Sopenharmony_ci					 reg, val);
283362306a36Sopenharmony_ci	}
283462306a36Sopenharmony_ci
283562306a36Sopenharmony_ci	return mdiobus_write(phydev->mdio.bus, phy_id, reg, val);
283662306a36Sopenharmony_ci}
283762306a36Sopenharmony_ci
283862306a36Sopenharmony_cistatic int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
283962306a36Sopenharmony_ci			    unsigned int reg)
284062306a36Sopenharmony_ci{
284162306a36Sopenharmony_ci	struct phylink_link_state state;
284262306a36Sopenharmony_ci	int val = 0xffff;
284362306a36Sopenharmony_ci
284462306a36Sopenharmony_ci	switch (pl->cur_link_an_mode) {
284562306a36Sopenharmony_ci	case MLO_AN_FIXED:
284662306a36Sopenharmony_ci		if (phy_id == 0) {
284762306a36Sopenharmony_ci			phylink_get_fixed_state(pl, &state);
284862306a36Sopenharmony_ci			val = phylink_mii_emul_read(reg, &state);
284962306a36Sopenharmony_ci		}
285062306a36Sopenharmony_ci		break;
285162306a36Sopenharmony_ci
285262306a36Sopenharmony_ci	case MLO_AN_PHY:
285362306a36Sopenharmony_ci		return -EOPNOTSUPP;
285462306a36Sopenharmony_ci
285562306a36Sopenharmony_ci	case MLO_AN_INBAND:
285662306a36Sopenharmony_ci		if (phy_id == 0) {
285762306a36Sopenharmony_ci			phylink_mac_pcs_get_state(pl, &state);
285862306a36Sopenharmony_ci			val = phylink_mii_emul_read(reg, &state);
285962306a36Sopenharmony_ci		}
286062306a36Sopenharmony_ci		break;
286162306a36Sopenharmony_ci	}
286262306a36Sopenharmony_ci
286362306a36Sopenharmony_ci	return val & 0xffff;
286462306a36Sopenharmony_ci}
286562306a36Sopenharmony_ci
286662306a36Sopenharmony_cistatic int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
286762306a36Sopenharmony_ci			     unsigned int reg, unsigned int val)
286862306a36Sopenharmony_ci{
286962306a36Sopenharmony_ci	switch (pl->cur_link_an_mode) {
287062306a36Sopenharmony_ci	case MLO_AN_FIXED:
287162306a36Sopenharmony_ci		break;
287262306a36Sopenharmony_ci
287362306a36Sopenharmony_ci	case MLO_AN_PHY:
287462306a36Sopenharmony_ci		return -EOPNOTSUPP;
287562306a36Sopenharmony_ci
287662306a36Sopenharmony_ci	case MLO_AN_INBAND:
287762306a36Sopenharmony_ci		break;
287862306a36Sopenharmony_ci	}
287962306a36Sopenharmony_ci
288062306a36Sopenharmony_ci	return 0;
288162306a36Sopenharmony_ci}
288262306a36Sopenharmony_ci
288362306a36Sopenharmony_ci/**
288462306a36Sopenharmony_ci * phylink_mii_ioctl() - generic mii ioctl interface
288562306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
288662306a36Sopenharmony_ci * @ifr: a pointer to a &struct ifreq for socket ioctls
288762306a36Sopenharmony_ci * @cmd: ioctl cmd to execute
288862306a36Sopenharmony_ci *
288962306a36Sopenharmony_ci * Perform the specified MII ioctl on the PHY attached to the phylink instance
289062306a36Sopenharmony_ci * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
289162306a36Sopenharmony_ci *
289262306a36Sopenharmony_ci * Returns: zero on success or negative error code.
289362306a36Sopenharmony_ci *
289462306a36Sopenharmony_ci * %SIOCGMIIPHY:
289562306a36Sopenharmony_ci *  read register from the current PHY.
289662306a36Sopenharmony_ci * %SIOCGMIIREG:
289762306a36Sopenharmony_ci *  read register from the specified PHY.
289862306a36Sopenharmony_ci * %SIOCSMIIREG:
289962306a36Sopenharmony_ci *  set a register on the specified PHY.
290062306a36Sopenharmony_ci */
290162306a36Sopenharmony_ciint phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
290262306a36Sopenharmony_ci{
290362306a36Sopenharmony_ci	struct mii_ioctl_data *mii = if_mii(ifr);
290462306a36Sopenharmony_ci	int  ret;
290562306a36Sopenharmony_ci
290662306a36Sopenharmony_ci	ASSERT_RTNL();
290762306a36Sopenharmony_ci
290862306a36Sopenharmony_ci	if (pl->phydev) {
290962306a36Sopenharmony_ci		/* PHYs only exist for MLO_AN_PHY and SGMII */
291062306a36Sopenharmony_ci		switch (cmd) {
291162306a36Sopenharmony_ci		case SIOCGMIIPHY:
291262306a36Sopenharmony_ci			mii->phy_id = pl->phydev->mdio.addr;
291362306a36Sopenharmony_ci			fallthrough;
291462306a36Sopenharmony_ci
291562306a36Sopenharmony_ci		case SIOCGMIIREG:
291662306a36Sopenharmony_ci			ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
291762306a36Sopenharmony_ci			if (ret >= 0) {
291862306a36Sopenharmony_ci				mii->val_out = ret;
291962306a36Sopenharmony_ci				ret = 0;
292062306a36Sopenharmony_ci			}
292162306a36Sopenharmony_ci			break;
292262306a36Sopenharmony_ci
292362306a36Sopenharmony_ci		case SIOCSMIIREG:
292462306a36Sopenharmony_ci			ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
292562306a36Sopenharmony_ci						mii->val_in);
292662306a36Sopenharmony_ci			break;
292762306a36Sopenharmony_ci
292862306a36Sopenharmony_ci		default:
292962306a36Sopenharmony_ci			ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
293062306a36Sopenharmony_ci			break;
293162306a36Sopenharmony_ci		}
293262306a36Sopenharmony_ci	} else {
293362306a36Sopenharmony_ci		switch (cmd) {
293462306a36Sopenharmony_ci		case SIOCGMIIPHY:
293562306a36Sopenharmony_ci			mii->phy_id = 0;
293662306a36Sopenharmony_ci			fallthrough;
293762306a36Sopenharmony_ci
293862306a36Sopenharmony_ci		case SIOCGMIIREG:
293962306a36Sopenharmony_ci			ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
294062306a36Sopenharmony_ci			if (ret >= 0) {
294162306a36Sopenharmony_ci				mii->val_out = ret;
294262306a36Sopenharmony_ci				ret = 0;
294362306a36Sopenharmony_ci			}
294462306a36Sopenharmony_ci			break;
294562306a36Sopenharmony_ci
294662306a36Sopenharmony_ci		case SIOCSMIIREG:
294762306a36Sopenharmony_ci			ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
294862306a36Sopenharmony_ci						mii->val_in);
294962306a36Sopenharmony_ci			break;
295062306a36Sopenharmony_ci
295162306a36Sopenharmony_ci		default:
295262306a36Sopenharmony_ci			ret = -EOPNOTSUPP;
295362306a36Sopenharmony_ci			break;
295462306a36Sopenharmony_ci		}
295562306a36Sopenharmony_ci	}
295662306a36Sopenharmony_ci
295762306a36Sopenharmony_ci	return ret;
295862306a36Sopenharmony_ci}
295962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_mii_ioctl);
296062306a36Sopenharmony_ci
296162306a36Sopenharmony_ci/**
296262306a36Sopenharmony_ci * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
296362306a36Sopenharmony_ci *   link partners
296462306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
296562306a36Sopenharmony_ci * @sync: perform action synchronously
296662306a36Sopenharmony_ci *
296762306a36Sopenharmony_ci * If we have a PHY that is not part of a SFP module, then set the speed
296862306a36Sopenharmony_ci * as described in the phy_speed_down() function. Please see this function
296962306a36Sopenharmony_ci * for a description of the @sync parameter.
297062306a36Sopenharmony_ci *
297162306a36Sopenharmony_ci * Returns zero if there is no PHY, otherwise as per phy_speed_down().
297262306a36Sopenharmony_ci */
297362306a36Sopenharmony_ciint phylink_speed_down(struct phylink *pl, bool sync)
297462306a36Sopenharmony_ci{
297562306a36Sopenharmony_ci	int ret = 0;
297662306a36Sopenharmony_ci
297762306a36Sopenharmony_ci	ASSERT_RTNL();
297862306a36Sopenharmony_ci
297962306a36Sopenharmony_ci	if (!pl->sfp_bus && pl->phydev)
298062306a36Sopenharmony_ci		ret = phy_speed_down(pl->phydev, sync);
298162306a36Sopenharmony_ci
298262306a36Sopenharmony_ci	return ret;
298362306a36Sopenharmony_ci}
298462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_speed_down);
298562306a36Sopenharmony_ci
298662306a36Sopenharmony_ci/**
298762306a36Sopenharmony_ci * phylink_speed_up() - restore the advertised speeds prior to the call to
298862306a36Sopenharmony_ci *   phylink_speed_down()
298962306a36Sopenharmony_ci * @pl: a pointer to a &struct phylink returned from phylink_create()
299062306a36Sopenharmony_ci *
299162306a36Sopenharmony_ci * If we have a PHY that is not part of a SFP module, then restore the
299262306a36Sopenharmony_ci * PHY speeds as per phy_speed_up().
299362306a36Sopenharmony_ci *
299462306a36Sopenharmony_ci * Returns zero if there is no PHY, otherwise as per phy_speed_up().
299562306a36Sopenharmony_ci */
299662306a36Sopenharmony_ciint phylink_speed_up(struct phylink *pl)
299762306a36Sopenharmony_ci{
299862306a36Sopenharmony_ci	int ret = 0;
299962306a36Sopenharmony_ci
300062306a36Sopenharmony_ci	ASSERT_RTNL();
300162306a36Sopenharmony_ci
300262306a36Sopenharmony_ci	if (!pl->sfp_bus && pl->phydev)
300362306a36Sopenharmony_ci		ret = phy_speed_up(pl->phydev);
300462306a36Sopenharmony_ci
300562306a36Sopenharmony_ci	return ret;
300662306a36Sopenharmony_ci}
300762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_speed_up);
300862306a36Sopenharmony_ci
300962306a36Sopenharmony_cistatic void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
301062306a36Sopenharmony_ci{
301162306a36Sopenharmony_ci	struct phylink *pl = upstream;
301262306a36Sopenharmony_ci
301362306a36Sopenharmony_ci	pl->netdev->sfp_bus = bus;
301462306a36Sopenharmony_ci}
301562306a36Sopenharmony_ci
301662306a36Sopenharmony_cistatic void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
301762306a36Sopenharmony_ci{
301862306a36Sopenharmony_ci	struct phylink *pl = upstream;
301962306a36Sopenharmony_ci
302062306a36Sopenharmony_ci	pl->netdev->sfp_bus = NULL;
302162306a36Sopenharmony_ci}
302262306a36Sopenharmony_ci
302362306a36Sopenharmony_cistatic const phy_interface_t phylink_sfp_interface_preference[] = {
302462306a36Sopenharmony_ci	PHY_INTERFACE_MODE_25GBASER,
302562306a36Sopenharmony_ci	PHY_INTERFACE_MODE_USXGMII,
302662306a36Sopenharmony_ci	PHY_INTERFACE_MODE_10GBASER,
302762306a36Sopenharmony_ci	PHY_INTERFACE_MODE_5GBASER,
302862306a36Sopenharmony_ci	PHY_INTERFACE_MODE_2500BASEX,
302962306a36Sopenharmony_ci	PHY_INTERFACE_MODE_SGMII,
303062306a36Sopenharmony_ci	PHY_INTERFACE_MODE_1000BASEX,
303162306a36Sopenharmony_ci	PHY_INTERFACE_MODE_100BASEX,
303262306a36Sopenharmony_ci};
303362306a36Sopenharmony_ci
303462306a36Sopenharmony_cistatic DECLARE_PHY_INTERFACE_MASK(phylink_sfp_interfaces);
303562306a36Sopenharmony_ci
303662306a36Sopenharmony_cistatic phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
303762306a36Sopenharmony_ci						    const unsigned long *intf)
303862306a36Sopenharmony_ci{
303962306a36Sopenharmony_ci	phy_interface_t interface;
304062306a36Sopenharmony_ci	size_t i;
304162306a36Sopenharmony_ci
304262306a36Sopenharmony_ci	interface = PHY_INTERFACE_MODE_NA;
304362306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++)
304462306a36Sopenharmony_ci		if (test_bit(phylink_sfp_interface_preference[i], intf)) {
304562306a36Sopenharmony_ci			interface = phylink_sfp_interface_preference[i];
304662306a36Sopenharmony_ci			break;
304762306a36Sopenharmony_ci		}
304862306a36Sopenharmony_ci
304962306a36Sopenharmony_ci	return interface;
305062306a36Sopenharmony_ci}
305162306a36Sopenharmony_ci
305262306a36Sopenharmony_cistatic void phylink_sfp_set_config(struct phylink *pl, u8 mode,
305362306a36Sopenharmony_ci				   unsigned long *supported,
305462306a36Sopenharmony_ci				   struct phylink_link_state *state)
305562306a36Sopenharmony_ci{
305662306a36Sopenharmony_ci	bool changed = false;
305762306a36Sopenharmony_ci
305862306a36Sopenharmony_ci	phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
305962306a36Sopenharmony_ci		    phylink_an_mode_str(mode), phy_modes(state->interface),
306062306a36Sopenharmony_ci		    __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
306162306a36Sopenharmony_ci
306262306a36Sopenharmony_ci	if (!linkmode_equal(pl->supported, supported)) {
306362306a36Sopenharmony_ci		linkmode_copy(pl->supported, supported);
306462306a36Sopenharmony_ci		changed = true;
306562306a36Sopenharmony_ci	}
306662306a36Sopenharmony_ci
306762306a36Sopenharmony_ci	if (!linkmode_equal(pl->link_config.advertising, state->advertising)) {
306862306a36Sopenharmony_ci		linkmode_copy(pl->link_config.advertising, state->advertising);
306962306a36Sopenharmony_ci		changed = true;
307062306a36Sopenharmony_ci	}
307162306a36Sopenharmony_ci
307262306a36Sopenharmony_ci	if (pl->cur_link_an_mode != mode ||
307362306a36Sopenharmony_ci	    pl->link_config.interface != state->interface) {
307462306a36Sopenharmony_ci		pl->cur_link_an_mode = mode;
307562306a36Sopenharmony_ci		pl->link_config.interface = state->interface;
307662306a36Sopenharmony_ci
307762306a36Sopenharmony_ci		changed = true;
307862306a36Sopenharmony_ci
307962306a36Sopenharmony_ci		phylink_info(pl, "switched to %s/%s link mode\n",
308062306a36Sopenharmony_ci			     phylink_an_mode_str(mode),
308162306a36Sopenharmony_ci			     phy_modes(state->interface));
308262306a36Sopenharmony_ci	}
308362306a36Sopenharmony_ci
308462306a36Sopenharmony_ci	if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
308562306a36Sopenharmony_ci				 &pl->phylink_disable_state))
308662306a36Sopenharmony_ci		phylink_mac_initial_config(pl, false);
308762306a36Sopenharmony_ci}
308862306a36Sopenharmony_ci
308962306a36Sopenharmony_cistatic int phylink_sfp_config_phy(struct phylink *pl, u8 mode,
309062306a36Sopenharmony_ci				  struct phy_device *phy)
309162306a36Sopenharmony_ci{
309262306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
309362306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
309462306a36Sopenharmony_ci	struct phylink_link_state config;
309562306a36Sopenharmony_ci	phy_interface_t iface;
309662306a36Sopenharmony_ci	int ret;
309762306a36Sopenharmony_ci
309862306a36Sopenharmony_ci	linkmode_copy(support, phy->supported);
309962306a36Sopenharmony_ci
310062306a36Sopenharmony_ci	memset(&config, 0, sizeof(config));
310162306a36Sopenharmony_ci	linkmode_copy(config.advertising, phy->advertising);
310262306a36Sopenharmony_ci	config.interface = PHY_INTERFACE_MODE_NA;
310362306a36Sopenharmony_ci	config.speed = SPEED_UNKNOWN;
310462306a36Sopenharmony_ci	config.duplex = DUPLEX_UNKNOWN;
310562306a36Sopenharmony_ci	config.pause = MLO_PAUSE_AN;
310662306a36Sopenharmony_ci
310762306a36Sopenharmony_ci	/* Ignore errors if we're expecting a PHY to attach later */
310862306a36Sopenharmony_ci	ret = phylink_validate(pl, support, &config);
310962306a36Sopenharmony_ci	if (ret) {
311062306a36Sopenharmony_ci		phylink_err(pl, "validation with support %*pb failed: %pe\n",
311162306a36Sopenharmony_ci			    __ETHTOOL_LINK_MODE_MASK_NBITS, support,
311262306a36Sopenharmony_ci			    ERR_PTR(ret));
311362306a36Sopenharmony_ci		return ret;
311462306a36Sopenharmony_ci	}
311562306a36Sopenharmony_ci
311662306a36Sopenharmony_ci	iface = sfp_select_interface(pl->sfp_bus, config.advertising);
311762306a36Sopenharmony_ci	if (iface == PHY_INTERFACE_MODE_NA) {
311862306a36Sopenharmony_ci		phylink_err(pl,
311962306a36Sopenharmony_ci			    "selection of interface failed, advertisement %*pb\n",
312062306a36Sopenharmony_ci			    __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
312162306a36Sopenharmony_ci		return -EINVAL;
312262306a36Sopenharmony_ci	}
312362306a36Sopenharmony_ci
312462306a36Sopenharmony_ci	config.interface = iface;
312562306a36Sopenharmony_ci	linkmode_copy(support1, support);
312662306a36Sopenharmony_ci	ret = phylink_validate(pl, support1, &config);
312762306a36Sopenharmony_ci	if (ret) {
312862306a36Sopenharmony_ci		phylink_err(pl,
312962306a36Sopenharmony_ci			    "validation of %s/%s with support %*pb failed: %pe\n",
313062306a36Sopenharmony_ci			    phylink_an_mode_str(mode),
313162306a36Sopenharmony_ci			    phy_modes(config.interface),
313262306a36Sopenharmony_ci			    __ETHTOOL_LINK_MODE_MASK_NBITS, support,
313362306a36Sopenharmony_ci			    ERR_PTR(ret));
313462306a36Sopenharmony_ci		return ret;
313562306a36Sopenharmony_ci	}
313662306a36Sopenharmony_ci
313762306a36Sopenharmony_ci	pl->link_port = pl->sfp_port;
313862306a36Sopenharmony_ci
313962306a36Sopenharmony_ci	phylink_sfp_set_config(pl, mode, support, &config);
314062306a36Sopenharmony_ci
314162306a36Sopenharmony_ci	return 0;
314262306a36Sopenharmony_ci}
314362306a36Sopenharmony_ci
314462306a36Sopenharmony_cistatic int phylink_sfp_config_optical(struct phylink *pl)
314562306a36Sopenharmony_ci{
314662306a36Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
314762306a36Sopenharmony_ci	DECLARE_PHY_INTERFACE_MASK(interfaces);
314862306a36Sopenharmony_ci	struct phylink_link_state config;
314962306a36Sopenharmony_ci	phy_interface_t interface;
315062306a36Sopenharmony_ci	int ret;
315162306a36Sopenharmony_ci
315262306a36Sopenharmony_ci	phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n",
315362306a36Sopenharmony_ci		    (int)PHY_INTERFACE_MODE_MAX,
315462306a36Sopenharmony_ci		    pl->config->supported_interfaces,
315562306a36Sopenharmony_ci		    (int)PHY_INTERFACE_MODE_MAX,
315662306a36Sopenharmony_ci		    pl->sfp_interfaces);
315762306a36Sopenharmony_ci
315862306a36Sopenharmony_ci	/* Find the union of the supported interfaces by the PCS/MAC and
315962306a36Sopenharmony_ci	 * the SFP module.
316062306a36Sopenharmony_ci	 */
316162306a36Sopenharmony_ci	phy_interface_and(interfaces, pl->config->supported_interfaces,
316262306a36Sopenharmony_ci			  pl->sfp_interfaces);
316362306a36Sopenharmony_ci	if (phy_interface_empty(interfaces)) {
316462306a36Sopenharmony_ci		phylink_err(pl, "unsupported SFP module: no common interface modes\n");
316562306a36Sopenharmony_ci		return -EINVAL;
316662306a36Sopenharmony_ci	}
316762306a36Sopenharmony_ci
316862306a36Sopenharmony_ci	memset(&config, 0, sizeof(config));
316962306a36Sopenharmony_ci	linkmode_copy(support, pl->sfp_support);
317062306a36Sopenharmony_ci	linkmode_copy(config.advertising, pl->sfp_support);
317162306a36Sopenharmony_ci	config.speed = SPEED_UNKNOWN;
317262306a36Sopenharmony_ci	config.duplex = DUPLEX_UNKNOWN;
317362306a36Sopenharmony_ci	config.pause = MLO_PAUSE_AN;
317462306a36Sopenharmony_ci
317562306a36Sopenharmony_ci	/* For all the interfaces that are supported, reduce the sfp_support
317662306a36Sopenharmony_ci	 * mask to only those link modes that can be supported.
317762306a36Sopenharmony_ci	 */
317862306a36Sopenharmony_ci	ret = phylink_validate_mask(pl, pl->sfp_support, &config, interfaces);
317962306a36Sopenharmony_ci	if (ret) {
318062306a36Sopenharmony_ci		phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
318162306a36Sopenharmony_ci			    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
318262306a36Sopenharmony_ci		return ret;
318362306a36Sopenharmony_ci	}
318462306a36Sopenharmony_ci
318562306a36Sopenharmony_ci	interface = phylink_choose_sfp_interface(pl, interfaces);
318662306a36Sopenharmony_ci	if (interface == PHY_INTERFACE_MODE_NA) {
318762306a36Sopenharmony_ci		phylink_err(pl, "failed to select SFP interface\n");
318862306a36Sopenharmony_ci		return -EINVAL;
318962306a36Sopenharmony_ci	}
319062306a36Sopenharmony_ci
319162306a36Sopenharmony_ci	phylink_dbg(pl, "optical SFP: chosen %s interface\n",
319262306a36Sopenharmony_ci		    phy_modes(interface));
319362306a36Sopenharmony_ci
319462306a36Sopenharmony_ci	config.interface = interface;
319562306a36Sopenharmony_ci
319662306a36Sopenharmony_ci	/* Ignore errors if we're expecting a PHY to attach later */
319762306a36Sopenharmony_ci	ret = phylink_validate(pl, support, &config);
319862306a36Sopenharmony_ci	if (ret) {
319962306a36Sopenharmony_ci		phylink_err(pl, "validation with support %*pb failed: %pe\n",
320062306a36Sopenharmony_ci			    __ETHTOOL_LINK_MODE_MASK_NBITS, support,
320162306a36Sopenharmony_ci			    ERR_PTR(ret));
320262306a36Sopenharmony_ci		return ret;
320362306a36Sopenharmony_ci	}
320462306a36Sopenharmony_ci
320562306a36Sopenharmony_ci	pl->link_port = pl->sfp_port;
320662306a36Sopenharmony_ci
320762306a36Sopenharmony_ci	phylink_sfp_set_config(pl, MLO_AN_INBAND, pl->sfp_support, &config);
320862306a36Sopenharmony_ci
320962306a36Sopenharmony_ci	return 0;
321062306a36Sopenharmony_ci}
321162306a36Sopenharmony_ci
321262306a36Sopenharmony_cistatic int phylink_sfp_module_insert(void *upstream,
321362306a36Sopenharmony_ci				     const struct sfp_eeprom_id *id)
321462306a36Sopenharmony_ci{
321562306a36Sopenharmony_ci	struct phylink *pl = upstream;
321662306a36Sopenharmony_ci
321762306a36Sopenharmony_ci	ASSERT_RTNL();
321862306a36Sopenharmony_ci
321962306a36Sopenharmony_ci	linkmode_zero(pl->sfp_support);
322062306a36Sopenharmony_ci	phy_interface_zero(pl->sfp_interfaces);
322162306a36Sopenharmony_ci	sfp_parse_support(pl->sfp_bus, id, pl->sfp_support, pl->sfp_interfaces);
322262306a36Sopenharmony_ci	pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, pl->sfp_support);
322362306a36Sopenharmony_ci
322462306a36Sopenharmony_ci	/* If this module may have a PHY connecting later, defer until later */
322562306a36Sopenharmony_ci	pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
322662306a36Sopenharmony_ci	if (pl->sfp_may_have_phy)
322762306a36Sopenharmony_ci		return 0;
322862306a36Sopenharmony_ci
322962306a36Sopenharmony_ci	return phylink_sfp_config_optical(pl);
323062306a36Sopenharmony_ci}
323162306a36Sopenharmony_ci
323262306a36Sopenharmony_cistatic int phylink_sfp_module_start(void *upstream)
323362306a36Sopenharmony_ci{
323462306a36Sopenharmony_ci	struct phylink *pl = upstream;
323562306a36Sopenharmony_ci
323662306a36Sopenharmony_ci	/* If this SFP module has a PHY, start the PHY now. */
323762306a36Sopenharmony_ci	if (pl->phydev) {
323862306a36Sopenharmony_ci		phy_start(pl->phydev);
323962306a36Sopenharmony_ci		return 0;
324062306a36Sopenharmony_ci	}
324162306a36Sopenharmony_ci
324262306a36Sopenharmony_ci	/* If the module may have a PHY but we didn't detect one we
324362306a36Sopenharmony_ci	 * need to configure the MAC here.
324462306a36Sopenharmony_ci	 */
324562306a36Sopenharmony_ci	if (!pl->sfp_may_have_phy)
324662306a36Sopenharmony_ci		return 0;
324762306a36Sopenharmony_ci
324862306a36Sopenharmony_ci	return phylink_sfp_config_optical(pl);
324962306a36Sopenharmony_ci}
325062306a36Sopenharmony_ci
325162306a36Sopenharmony_cistatic void phylink_sfp_module_stop(void *upstream)
325262306a36Sopenharmony_ci{
325362306a36Sopenharmony_ci	struct phylink *pl = upstream;
325462306a36Sopenharmony_ci
325562306a36Sopenharmony_ci	/* If this SFP module has a PHY, stop it. */
325662306a36Sopenharmony_ci	if (pl->phydev)
325762306a36Sopenharmony_ci		phy_stop(pl->phydev);
325862306a36Sopenharmony_ci}
325962306a36Sopenharmony_ci
326062306a36Sopenharmony_cistatic void phylink_sfp_link_down(void *upstream)
326162306a36Sopenharmony_ci{
326262306a36Sopenharmony_ci	struct phylink *pl = upstream;
326362306a36Sopenharmony_ci
326462306a36Sopenharmony_ci	ASSERT_RTNL();
326562306a36Sopenharmony_ci
326662306a36Sopenharmony_ci	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
326762306a36Sopenharmony_ci}
326862306a36Sopenharmony_ci
326962306a36Sopenharmony_cistatic void phylink_sfp_link_up(void *upstream)
327062306a36Sopenharmony_ci{
327162306a36Sopenharmony_ci	struct phylink *pl = upstream;
327262306a36Sopenharmony_ci
327362306a36Sopenharmony_ci	ASSERT_RTNL();
327462306a36Sopenharmony_ci
327562306a36Sopenharmony_ci	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
327662306a36Sopenharmony_ci}
327762306a36Sopenharmony_ci
327862306a36Sopenharmony_ci/* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
327962306a36Sopenharmony_ci * or 802.3z control word, so inband will not work.
328062306a36Sopenharmony_ci */
328162306a36Sopenharmony_cistatic bool phylink_phy_no_inband(struct phy_device *phy)
328262306a36Sopenharmony_ci{
328362306a36Sopenharmony_ci	return phy->is_c45 && phy_id_compare(phy->c45_ids.device_ids[1],
328462306a36Sopenharmony_ci					     0xae025150, 0xfffffff0);
328562306a36Sopenharmony_ci}
328662306a36Sopenharmony_ci
328762306a36Sopenharmony_cistatic int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
328862306a36Sopenharmony_ci{
328962306a36Sopenharmony_ci	struct phylink *pl = upstream;
329062306a36Sopenharmony_ci	phy_interface_t interface;
329162306a36Sopenharmony_ci	u8 mode;
329262306a36Sopenharmony_ci	int ret;
329362306a36Sopenharmony_ci
329462306a36Sopenharmony_ci	/*
329562306a36Sopenharmony_ci	 * This is the new way of dealing with flow control for PHYs,
329662306a36Sopenharmony_ci	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
329762306a36Sopenharmony_ci	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
329862306a36Sopenharmony_ci	 * using our validate call to the MAC, we rely upon the MAC
329962306a36Sopenharmony_ci	 * clearing the bits from both supported and advertising fields.
330062306a36Sopenharmony_ci	 */
330162306a36Sopenharmony_ci	phy_support_asym_pause(phy);
330262306a36Sopenharmony_ci
330362306a36Sopenharmony_ci	if (phylink_phy_no_inband(phy))
330462306a36Sopenharmony_ci		mode = MLO_AN_PHY;
330562306a36Sopenharmony_ci	else
330662306a36Sopenharmony_ci		mode = MLO_AN_INBAND;
330762306a36Sopenharmony_ci
330862306a36Sopenharmony_ci	/* Set the PHY's host supported interfaces */
330962306a36Sopenharmony_ci	phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
331062306a36Sopenharmony_ci			  pl->config->supported_interfaces);
331162306a36Sopenharmony_ci
331262306a36Sopenharmony_ci	/* Do the initial configuration */
331362306a36Sopenharmony_ci	ret = phylink_sfp_config_phy(pl, mode, phy);
331462306a36Sopenharmony_ci	if (ret < 0)
331562306a36Sopenharmony_ci		return ret;
331662306a36Sopenharmony_ci
331762306a36Sopenharmony_ci	interface = pl->link_config.interface;
331862306a36Sopenharmony_ci	ret = phylink_attach_phy(pl, phy, interface);
331962306a36Sopenharmony_ci	if (ret < 0)
332062306a36Sopenharmony_ci		return ret;
332162306a36Sopenharmony_ci
332262306a36Sopenharmony_ci	ret = phylink_bringup_phy(pl, phy, interface);
332362306a36Sopenharmony_ci	if (ret)
332462306a36Sopenharmony_ci		phy_detach(phy);
332562306a36Sopenharmony_ci
332662306a36Sopenharmony_ci	return ret;
332762306a36Sopenharmony_ci}
332862306a36Sopenharmony_ci
332962306a36Sopenharmony_cistatic void phylink_sfp_disconnect_phy(void *upstream)
333062306a36Sopenharmony_ci{
333162306a36Sopenharmony_ci	phylink_disconnect_phy(upstream);
333262306a36Sopenharmony_ci}
333362306a36Sopenharmony_ci
333462306a36Sopenharmony_cistatic const struct sfp_upstream_ops sfp_phylink_ops = {
333562306a36Sopenharmony_ci	.attach = phylink_sfp_attach,
333662306a36Sopenharmony_ci	.detach = phylink_sfp_detach,
333762306a36Sopenharmony_ci	.module_insert = phylink_sfp_module_insert,
333862306a36Sopenharmony_ci	.module_start = phylink_sfp_module_start,
333962306a36Sopenharmony_ci	.module_stop = phylink_sfp_module_stop,
334062306a36Sopenharmony_ci	.link_up = phylink_sfp_link_up,
334162306a36Sopenharmony_ci	.link_down = phylink_sfp_link_down,
334262306a36Sopenharmony_ci	.connect_phy = phylink_sfp_connect_phy,
334362306a36Sopenharmony_ci	.disconnect_phy = phylink_sfp_disconnect_phy,
334462306a36Sopenharmony_ci};
334562306a36Sopenharmony_ci
334662306a36Sopenharmony_ci/* Helpers for MAC drivers */
334762306a36Sopenharmony_ci
334862306a36Sopenharmony_cistatic struct {
334962306a36Sopenharmony_ci	int bit;
335062306a36Sopenharmony_ci	int speed;
335162306a36Sopenharmony_ci} phylink_c73_priority_resolution[] = {
335262306a36Sopenharmony_ci	{ ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, SPEED_100000 },
335362306a36Sopenharmony_ci	{ ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, SPEED_100000 },
335462306a36Sopenharmony_ci	/* 100GBASE-KP4 and 100GBASE-CR10 not supported */
335562306a36Sopenharmony_ci	{ ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, SPEED_40000 },
335662306a36Sopenharmony_ci	{ ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, SPEED_40000 },
335762306a36Sopenharmony_ci	{ ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, SPEED_10000 },
335862306a36Sopenharmony_ci	{ ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, SPEED_10000 },
335962306a36Sopenharmony_ci	/* 5GBASE-KR not supported */
336062306a36Sopenharmony_ci	{ ETHTOOL_LINK_MODE_2500baseX_Full_BIT, SPEED_2500 },
336162306a36Sopenharmony_ci	{ ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, SPEED_1000 },
336262306a36Sopenharmony_ci};
336362306a36Sopenharmony_ci
336462306a36Sopenharmony_civoid phylink_resolve_c73(struct phylink_link_state *state)
336562306a36Sopenharmony_ci{
336662306a36Sopenharmony_ci	int i;
336762306a36Sopenharmony_ci
336862306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(phylink_c73_priority_resolution); i++) {
336962306a36Sopenharmony_ci		int bit = phylink_c73_priority_resolution[i].bit;
337062306a36Sopenharmony_ci		if (linkmode_test_bit(bit, state->advertising) &&
337162306a36Sopenharmony_ci		    linkmode_test_bit(bit, state->lp_advertising))
337262306a36Sopenharmony_ci			break;
337362306a36Sopenharmony_ci	}
337462306a36Sopenharmony_ci
337562306a36Sopenharmony_ci	if (i < ARRAY_SIZE(phylink_c73_priority_resolution)) {
337662306a36Sopenharmony_ci		state->speed = phylink_c73_priority_resolution[i].speed;
337762306a36Sopenharmony_ci		state->duplex = DUPLEX_FULL;
337862306a36Sopenharmony_ci	} else {
337962306a36Sopenharmony_ci		/* negotiation failure */
338062306a36Sopenharmony_ci		state->link = false;
338162306a36Sopenharmony_ci	}
338262306a36Sopenharmony_ci
338362306a36Sopenharmony_ci	phylink_resolve_an_pause(state);
338462306a36Sopenharmony_ci}
338562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_resolve_c73);
338662306a36Sopenharmony_ci
338762306a36Sopenharmony_cistatic void phylink_decode_c37_word(struct phylink_link_state *state,
338862306a36Sopenharmony_ci				    uint16_t config_reg, int speed)
338962306a36Sopenharmony_ci{
339062306a36Sopenharmony_ci	int fd_bit;
339162306a36Sopenharmony_ci
339262306a36Sopenharmony_ci	if (speed == SPEED_2500)
339362306a36Sopenharmony_ci		fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
339462306a36Sopenharmony_ci	else
339562306a36Sopenharmony_ci		fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
339662306a36Sopenharmony_ci
339762306a36Sopenharmony_ci	mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
339862306a36Sopenharmony_ci
339962306a36Sopenharmony_ci	if (linkmode_test_bit(fd_bit, state->advertising) &&
340062306a36Sopenharmony_ci	    linkmode_test_bit(fd_bit, state->lp_advertising)) {
340162306a36Sopenharmony_ci		state->speed = speed;
340262306a36Sopenharmony_ci		state->duplex = DUPLEX_FULL;
340362306a36Sopenharmony_ci	} else {
340462306a36Sopenharmony_ci		/* negotiation failure */
340562306a36Sopenharmony_ci		state->link = false;
340662306a36Sopenharmony_ci	}
340762306a36Sopenharmony_ci
340862306a36Sopenharmony_ci	phylink_resolve_an_pause(state);
340962306a36Sopenharmony_ci}
341062306a36Sopenharmony_ci
341162306a36Sopenharmony_cistatic void phylink_decode_sgmii_word(struct phylink_link_state *state,
341262306a36Sopenharmony_ci				      uint16_t config_reg)
341362306a36Sopenharmony_ci{
341462306a36Sopenharmony_ci	if (!(config_reg & LPA_SGMII_LINK)) {
341562306a36Sopenharmony_ci		state->link = false;
341662306a36Sopenharmony_ci		return;
341762306a36Sopenharmony_ci	}
341862306a36Sopenharmony_ci
341962306a36Sopenharmony_ci	switch (config_reg & LPA_SGMII_SPD_MASK) {
342062306a36Sopenharmony_ci	case LPA_SGMII_10:
342162306a36Sopenharmony_ci		state->speed = SPEED_10;
342262306a36Sopenharmony_ci		break;
342362306a36Sopenharmony_ci	case LPA_SGMII_100:
342462306a36Sopenharmony_ci		state->speed = SPEED_100;
342562306a36Sopenharmony_ci		break;
342662306a36Sopenharmony_ci	case LPA_SGMII_1000:
342762306a36Sopenharmony_ci		state->speed = SPEED_1000;
342862306a36Sopenharmony_ci		break;
342962306a36Sopenharmony_ci	default:
343062306a36Sopenharmony_ci		state->link = false;
343162306a36Sopenharmony_ci		return;
343262306a36Sopenharmony_ci	}
343362306a36Sopenharmony_ci	if (config_reg & LPA_SGMII_FULL_DUPLEX)
343462306a36Sopenharmony_ci		state->duplex = DUPLEX_FULL;
343562306a36Sopenharmony_ci	else
343662306a36Sopenharmony_ci		state->duplex = DUPLEX_HALF;
343762306a36Sopenharmony_ci}
343862306a36Sopenharmony_ci
343962306a36Sopenharmony_ci/**
344062306a36Sopenharmony_ci * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
344162306a36Sopenharmony_ci * @state: a pointer to a struct phylink_link_state.
344262306a36Sopenharmony_ci * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
344362306a36Sopenharmony_ci *
344462306a36Sopenharmony_ci * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
344562306a36Sopenharmony_ci * code word.  Decode the USXGMII code word and populate the corresponding fields
344662306a36Sopenharmony_ci * (speed, duplex) into the phylink_link_state structure.
344762306a36Sopenharmony_ci */
344862306a36Sopenharmony_civoid phylink_decode_usxgmii_word(struct phylink_link_state *state,
344962306a36Sopenharmony_ci				 uint16_t lpa)
345062306a36Sopenharmony_ci{
345162306a36Sopenharmony_ci	switch (lpa & MDIO_USXGMII_SPD_MASK) {
345262306a36Sopenharmony_ci	case MDIO_USXGMII_10:
345362306a36Sopenharmony_ci		state->speed = SPEED_10;
345462306a36Sopenharmony_ci		break;
345562306a36Sopenharmony_ci	case MDIO_USXGMII_100:
345662306a36Sopenharmony_ci		state->speed = SPEED_100;
345762306a36Sopenharmony_ci		break;
345862306a36Sopenharmony_ci	case MDIO_USXGMII_1000:
345962306a36Sopenharmony_ci		state->speed = SPEED_1000;
346062306a36Sopenharmony_ci		break;
346162306a36Sopenharmony_ci	case MDIO_USXGMII_2500:
346262306a36Sopenharmony_ci		state->speed = SPEED_2500;
346362306a36Sopenharmony_ci		break;
346462306a36Sopenharmony_ci	case MDIO_USXGMII_5000:
346562306a36Sopenharmony_ci		state->speed = SPEED_5000;
346662306a36Sopenharmony_ci		break;
346762306a36Sopenharmony_ci	case MDIO_USXGMII_10G:
346862306a36Sopenharmony_ci		state->speed = SPEED_10000;
346962306a36Sopenharmony_ci		break;
347062306a36Sopenharmony_ci	default:
347162306a36Sopenharmony_ci		state->link = false;
347262306a36Sopenharmony_ci		return;
347362306a36Sopenharmony_ci	}
347462306a36Sopenharmony_ci
347562306a36Sopenharmony_ci	if (lpa & MDIO_USXGMII_FULL_DUPLEX)
347662306a36Sopenharmony_ci		state->duplex = DUPLEX_FULL;
347762306a36Sopenharmony_ci	else
347862306a36Sopenharmony_ci		state->duplex = DUPLEX_HALF;
347962306a36Sopenharmony_ci}
348062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
348162306a36Sopenharmony_ci
348262306a36Sopenharmony_ci/**
348362306a36Sopenharmony_ci * phylink_decode_usgmii_word() - decode the USGMII word from a MAC PCS
348462306a36Sopenharmony_ci * @state: a pointer to a struct phylink_link_state.
348562306a36Sopenharmony_ci * @lpa: a 16 bit value which stores the USGMII auto-negotiation word
348662306a36Sopenharmony_ci *
348762306a36Sopenharmony_ci * Helper for MAC PCS supporting the USGMII protocol and the auto-negotiation
348862306a36Sopenharmony_ci * code word.  Decode the USGMII code word and populate the corresponding fields
348962306a36Sopenharmony_ci * (speed, duplex) into the phylink_link_state structure. The structure for this
349062306a36Sopenharmony_ci * word is the same as the USXGMII word, except it only supports speeds up to
349162306a36Sopenharmony_ci * 1Gbps.
349262306a36Sopenharmony_ci */
349362306a36Sopenharmony_cistatic void phylink_decode_usgmii_word(struct phylink_link_state *state,
349462306a36Sopenharmony_ci				       uint16_t lpa)
349562306a36Sopenharmony_ci{
349662306a36Sopenharmony_ci	switch (lpa & MDIO_USXGMII_SPD_MASK) {
349762306a36Sopenharmony_ci	case MDIO_USXGMII_10:
349862306a36Sopenharmony_ci		state->speed = SPEED_10;
349962306a36Sopenharmony_ci		break;
350062306a36Sopenharmony_ci	case MDIO_USXGMII_100:
350162306a36Sopenharmony_ci		state->speed = SPEED_100;
350262306a36Sopenharmony_ci		break;
350362306a36Sopenharmony_ci	case MDIO_USXGMII_1000:
350462306a36Sopenharmony_ci		state->speed = SPEED_1000;
350562306a36Sopenharmony_ci		break;
350662306a36Sopenharmony_ci	default:
350762306a36Sopenharmony_ci		state->link = false;
350862306a36Sopenharmony_ci		return;
350962306a36Sopenharmony_ci	}
351062306a36Sopenharmony_ci
351162306a36Sopenharmony_ci	if (lpa & MDIO_USXGMII_FULL_DUPLEX)
351262306a36Sopenharmony_ci		state->duplex = DUPLEX_FULL;
351362306a36Sopenharmony_ci	else
351462306a36Sopenharmony_ci		state->duplex = DUPLEX_HALF;
351562306a36Sopenharmony_ci}
351662306a36Sopenharmony_ci
351762306a36Sopenharmony_ci/**
351862306a36Sopenharmony_ci * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
351962306a36Sopenharmony_ci * @state: a pointer to a &struct phylink_link_state.
352062306a36Sopenharmony_ci * @bmsr: The value of the %MII_BMSR register
352162306a36Sopenharmony_ci * @lpa: The value of the %MII_LPA register
352262306a36Sopenharmony_ci *
352362306a36Sopenharmony_ci * Helper for MAC PCS supporting the 802.3 clause 22 register set for
352462306a36Sopenharmony_ci * clause 37 negotiation and/or SGMII control.
352562306a36Sopenharmony_ci *
352662306a36Sopenharmony_ci * Parse the Clause 37 or Cisco SGMII link partner negotiation word into
352762306a36Sopenharmony_ci * the phylink @state structure. This is suitable to be used for implementing
352862306a36Sopenharmony_ci * the pcs_get_state() member of the struct phylink_pcs_ops structure if
352962306a36Sopenharmony_ci * accessing @bmsr and @lpa cannot be done with MDIO directly.
353062306a36Sopenharmony_ci */
353162306a36Sopenharmony_civoid phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
353262306a36Sopenharmony_ci				      u16 bmsr, u16 lpa)
353362306a36Sopenharmony_ci{
353462306a36Sopenharmony_ci	state->link = !!(bmsr & BMSR_LSTATUS);
353562306a36Sopenharmony_ci	state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
353662306a36Sopenharmony_ci	/* If there is no link or autonegotiation is disabled, the LP advertisement
353762306a36Sopenharmony_ci	 * data is not meaningful, so don't go any further.
353862306a36Sopenharmony_ci	 */
353962306a36Sopenharmony_ci	if (!state->link || !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
354062306a36Sopenharmony_ci					       state->advertising))
354162306a36Sopenharmony_ci		return;
354262306a36Sopenharmony_ci
354362306a36Sopenharmony_ci	switch (state->interface) {
354462306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_1000BASEX:
354562306a36Sopenharmony_ci		phylink_decode_c37_word(state, lpa, SPEED_1000);
354662306a36Sopenharmony_ci		break;
354762306a36Sopenharmony_ci
354862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_2500BASEX:
354962306a36Sopenharmony_ci		phylink_decode_c37_word(state, lpa, SPEED_2500);
355062306a36Sopenharmony_ci		break;
355162306a36Sopenharmony_ci
355262306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_SGMII:
355362306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_QSGMII:
355462306a36Sopenharmony_ci		phylink_decode_sgmii_word(state, lpa);
355562306a36Sopenharmony_ci		break;
355662306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_QUSGMII:
355762306a36Sopenharmony_ci		phylink_decode_usgmii_word(state, lpa);
355862306a36Sopenharmony_ci		break;
355962306a36Sopenharmony_ci
356062306a36Sopenharmony_ci	default:
356162306a36Sopenharmony_ci		state->link = false;
356262306a36Sopenharmony_ci		break;
356362306a36Sopenharmony_ci	}
356462306a36Sopenharmony_ci}
356562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state);
356662306a36Sopenharmony_ci
356762306a36Sopenharmony_ci/**
356862306a36Sopenharmony_ci * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
356962306a36Sopenharmony_ci * @pcs: a pointer to a &struct mdio_device.
357062306a36Sopenharmony_ci * @state: a pointer to a &struct phylink_link_state.
357162306a36Sopenharmony_ci *
357262306a36Sopenharmony_ci * Helper for MAC PCS supporting the 802.3 clause 22 register set for
357362306a36Sopenharmony_ci * clause 37 negotiation and/or SGMII control.
357462306a36Sopenharmony_ci *
357562306a36Sopenharmony_ci * Read the MAC PCS state from the MII device configured in @config and
357662306a36Sopenharmony_ci * parse the Clause 37 or Cisco SGMII link partner negotiation word into
357762306a36Sopenharmony_ci * the phylink @state structure. This is suitable to be directly plugged
357862306a36Sopenharmony_ci * into the pcs_get_state() member of the struct phylink_pcs_ops
357962306a36Sopenharmony_ci * structure.
358062306a36Sopenharmony_ci */
358162306a36Sopenharmony_civoid phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
358262306a36Sopenharmony_ci				   struct phylink_link_state *state)
358362306a36Sopenharmony_ci{
358462306a36Sopenharmony_ci	int bmsr, lpa;
358562306a36Sopenharmony_ci
358662306a36Sopenharmony_ci	bmsr = mdiodev_read(pcs, MII_BMSR);
358762306a36Sopenharmony_ci	lpa = mdiodev_read(pcs, MII_LPA);
358862306a36Sopenharmony_ci	if (bmsr < 0 || lpa < 0) {
358962306a36Sopenharmony_ci		state->link = false;
359062306a36Sopenharmony_ci		return;
359162306a36Sopenharmony_ci	}
359262306a36Sopenharmony_ci
359362306a36Sopenharmony_ci	phylink_mii_c22_pcs_decode_state(state, bmsr, lpa);
359462306a36Sopenharmony_ci}
359562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
359662306a36Sopenharmony_ci
359762306a36Sopenharmony_ci/**
359862306a36Sopenharmony_ci * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
359962306a36Sopenharmony_ci *	advertisement
360062306a36Sopenharmony_ci * @interface: the PHY interface mode being configured
360162306a36Sopenharmony_ci * @advertising: the ethtool advertisement mask
360262306a36Sopenharmony_ci *
360362306a36Sopenharmony_ci * Helper for MAC PCS supporting the 802.3 clause 22 register set for
360462306a36Sopenharmony_ci * clause 37 negotiation and/or SGMII control.
360562306a36Sopenharmony_ci *
360662306a36Sopenharmony_ci * Encode the clause 37 PCS advertisement as specified by @interface and
360762306a36Sopenharmony_ci * @advertising.
360862306a36Sopenharmony_ci *
360962306a36Sopenharmony_ci * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
361062306a36Sopenharmony_ci */
361162306a36Sopenharmony_ciint phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
361262306a36Sopenharmony_ci					     const unsigned long *advertising)
361362306a36Sopenharmony_ci{
361462306a36Sopenharmony_ci	u16 adv;
361562306a36Sopenharmony_ci
361662306a36Sopenharmony_ci	switch (interface) {
361762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_1000BASEX:
361862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_2500BASEX:
361962306a36Sopenharmony_ci		adv = ADVERTISE_1000XFULL;
362062306a36Sopenharmony_ci		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
362162306a36Sopenharmony_ci				      advertising))
362262306a36Sopenharmony_ci			adv |= ADVERTISE_1000XPAUSE;
362362306a36Sopenharmony_ci		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
362462306a36Sopenharmony_ci				      advertising))
362562306a36Sopenharmony_ci			adv |= ADVERTISE_1000XPSE_ASYM;
362662306a36Sopenharmony_ci		return adv;
362762306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_SGMII:
362862306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_QSGMII:
362962306a36Sopenharmony_ci		return 0x0001;
363062306a36Sopenharmony_ci	default:
363162306a36Sopenharmony_ci		/* Nothing to do for other modes */
363262306a36Sopenharmony_ci		return -EINVAL;
363362306a36Sopenharmony_ci	}
363462306a36Sopenharmony_ci}
363562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement);
363662306a36Sopenharmony_ci
363762306a36Sopenharmony_ci/**
363862306a36Sopenharmony_ci * phylink_mii_c22_pcs_config() - configure clause 22 PCS
363962306a36Sopenharmony_ci * @pcs: a pointer to a &struct mdio_device.
364062306a36Sopenharmony_ci * @interface: the PHY interface mode being configured
364162306a36Sopenharmony_ci * @advertising: the ethtool advertisement mask
364262306a36Sopenharmony_ci * @neg_mode: PCS negotiation mode
364362306a36Sopenharmony_ci *
364462306a36Sopenharmony_ci * Configure a Clause 22 PCS PHY with the appropriate negotiation
364562306a36Sopenharmony_ci * parameters for the @mode, @interface and @advertising parameters.
364662306a36Sopenharmony_ci * Returns negative error number on failure, zero if the advertisement
364762306a36Sopenharmony_ci * has not changed, or positive if there is a change.
364862306a36Sopenharmony_ci */
364962306a36Sopenharmony_ciint phylink_mii_c22_pcs_config(struct mdio_device *pcs,
365062306a36Sopenharmony_ci			       phy_interface_t interface,
365162306a36Sopenharmony_ci			       const unsigned long *advertising,
365262306a36Sopenharmony_ci			       unsigned int neg_mode)
365362306a36Sopenharmony_ci{
365462306a36Sopenharmony_ci	bool changed = 0;
365562306a36Sopenharmony_ci	u16 bmcr;
365662306a36Sopenharmony_ci	int ret, adv;
365762306a36Sopenharmony_ci
365862306a36Sopenharmony_ci	adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
365962306a36Sopenharmony_ci	if (adv >= 0) {
366062306a36Sopenharmony_ci		ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
366162306a36Sopenharmony_ci					     MII_ADVERTISE, 0xffff, adv);
366262306a36Sopenharmony_ci		if (ret < 0)
366362306a36Sopenharmony_ci			return ret;
366462306a36Sopenharmony_ci		changed = ret;
366562306a36Sopenharmony_ci	}
366662306a36Sopenharmony_ci
366762306a36Sopenharmony_ci	if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
366862306a36Sopenharmony_ci		bmcr = BMCR_ANENABLE;
366962306a36Sopenharmony_ci	else
367062306a36Sopenharmony_ci		bmcr = 0;
367162306a36Sopenharmony_ci
367262306a36Sopenharmony_ci	/* Configure the inband state. Ensure ISOLATE bit is disabled */
367362306a36Sopenharmony_ci	ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
367462306a36Sopenharmony_ci	if (ret < 0)
367562306a36Sopenharmony_ci		return ret;
367662306a36Sopenharmony_ci
367762306a36Sopenharmony_ci	return changed;
367862306a36Sopenharmony_ci}
367962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
368062306a36Sopenharmony_ci
368162306a36Sopenharmony_ci/**
368262306a36Sopenharmony_ci * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
368362306a36Sopenharmony_ci * @pcs: a pointer to a &struct mdio_device.
368462306a36Sopenharmony_ci *
368562306a36Sopenharmony_ci * Helper for MAC PCS supporting the 802.3 clause 22 register set for
368662306a36Sopenharmony_ci * clause 37 negotiation.
368762306a36Sopenharmony_ci *
368862306a36Sopenharmony_ci * Restart the clause 37 negotiation with the link partner. This is
368962306a36Sopenharmony_ci * suitable to be directly plugged into the pcs_get_state() member
369062306a36Sopenharmony_ci * of the struct phylink_pcs_ops structure.
369162306a36Sopenharmony_ci */
369262306a36Sopenharmony_civoid phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
369362306a36Sopenharmony_ci{
369462306a36Sopenharmony_ci	int val = mdiodev_read(pcs, MII_BMCR);
369562306a36Sopenharmony_ci
369662306a36Sopenharmony_ci	if (val >= 0) {
369762306a36Sopenharmony_ci		val |= BMCR_ANRESTART;
369862306a36Sopenharmony_ci
369962306a36Sopenharmony_ci		mdiodev_write(pcs, MII_BMCR, val);
370062306a36Sopenharmony_ci	}
370162306a36Sopenharmony_ci}
370262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
370362306a36Sopenharmony_ci
370462306a36Sopenharmony_civoid phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
370562306a36Sopenharmony_ci				   struct phylink_link_state *state)
370662306a36Sopenharmony_ci{
370762306a36Sopenharmony_ci	struct mii_bus *bus = pcs->bus;
370862306a36Sopenharmony_ci	int addr = pcs->addr;
370962306a36Sopenharmony_ci	int stat;
371062306a36Sopenharmony_ci
371162306a36Sopenharmony_ci	stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
371262306a36Sopenharmony_ci	if (stat < 0) {
371362306a36Sopenharmony_ci		state->link = false;
371462306a36Sopenharmony_ci		return;
371562306a36Sopenharmony_ci	}
371662306a36Sopenharmony_ci
371762306a36Sopenharmony_ci	state->link = !!(stat & MDIO_STAT1_LSTATUS);
371862306a36Sopenharmony_ci	if (!state->link)
371962306a36Sopenharmony_ci		return;
372062306a36Sopenharmony_ci
372162306a36Sopenharmony_ci	switch (state->interface) {
372262306a36Sopenharmony_ci	case PHY_INTERFACE_MODE_10GBASER:
372362306a36Sopenharmony_ci		state->speed = SPEED_10000;
372462306a36Sopenharmony_ci		state->duplex = DUPLEX_FULL;
372562306a36Sopenharmony_ci		break;
372662306a36Sopenharmony_ci
372762306a36Sopenharmony_ci	default:
372862306a36Sopenharmony_ci		break;
372962306a36Sopenharmony_ci	}
373062306a36Sopenharmony_ci}
373162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
373262306a36Sopenharmony_ci
373362306a36Sopenharmony_cistatic int __init phylink_init(void)
373462306a36Sopenharmony_ci{
373562306a36Sopenharmony_ci	for (int i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); ++i)
373662306a36Sopenharmony_ci		__set_bit(phylink_sfp_interface_preference[i],
373762306a36Sopenharmony_ci			  phylink_sfp_interfaces);
373862306a36Sopenharmony_ci
373962306a36Sopenharmony_ci	return 0;
374062306a36Sopenharmony_ci}
374162306a36Sopenharmony_ci
374262306a36Sopenharmony_cimodule_init(phylink_init);
374362306a36Sopenharmony_ci
374462306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
3745