1 From d25f3a74f30aace819163dfa54f2a4b8ca1dc932 Mon Sep 17 00:00:00 2001
2 From: "Russell King (Oracle)" <rmk+kernel@armlinux.org.uk>
3 Date: Tue, 26 Oct 2021 11:06:11 +0100
4 Subject: [PATCH] net: phylink: use supported_interfaces for phylink
7 If the network device supplies a supported interface bitmap, we can use
8 that during phylink's validation to simplify MAC drivers in two ways by
9 using the supported_interfaces bitmap to:
11 1. reject unsupported interfaces before calling into the MAC driver.
12 2. generate the set of all supported link modes across all supported
13 interfaces (used mainly for SFP, but also some 10G PHYs.)
15 Suggested-by: Sean Anderson <sean.anderson@seco.com>
16 Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
17 Signed-off-by: David S. Miller <davem@davemloft.net>
19 drivers/net/phy/phylink.c | 36 ++++++++++++++++++++++++++++++++++++
20 include/linux/phylink.h | 12 ++++++++++--
21 2 files changed, 46 insertions(+), 2 deletions(-)
23 --- a/drivers/net/phy/phylink.c
24 +++ b/drivers/net/phy/phylink.c
25 @@ -155,9 +155,45 @@ static const char *phylink_an_mode_str(u
26 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
29 +static int phylink_validate_any(struct phylink *pl, unsigned long *supported,
30 + struct phylink_link_state *state)
32 + __ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
33 + __ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
34 + __ETHTOOL_DECLARE_LINK_MODE_MASK(s);
35 + struct phylink_link_state t;
38 + for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) {
39 + if (test_bit(intf, pl->config->supported_interfaces)) {
40 + linkmode_copy(s, supported);
44 + pl->mac_ops->validate(pl->config, s, &t);
45 + linkmode_or(all_s, all_s, s);
46 + linkmode_or(all_adv, all_adv, t.advertising);
50 + linkmode_copy(supported, all_s);
51 + linkmode_copy(state->advertising, all_adv);
53 + return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
56 static int phylink_validate(struct phylink *pl, unsigned long *supported,
57 struct phylink_link_state *state)
59 + if (!phy_interface_empty(pl->config->supported_interfaces)) {
60 + if (state->interface == PHY_INTERFACE_MODE_NA)
61 + return phylink_validate_any(pl, supported, state);
63 + if (!test_bit(state->interface,
64 + pl->config->supported_interfaces))
68 pl->mac_ops->validate(pl->config, supported, state);
70 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
71 --- a/include/linux/phylink.h
72 +++ b/include/linux/phylink.h
73 @@ -68,6 +68,8 @@ enum phylink_op_type {
74 * @ovr_an_inband: if true, override PCS to MLO_AN_INBAND
75 * @get_fixed_state: callback to execute to determine the fixed link state,
76 * if MAC link is at %MLO_AN_FIXED mode.
77 + * @supported_interfaces: bitmap describing which PHY_INTERFACE_MODE_xxx
78 + * are supported by the MAC/PCS.
80 struct phylink_config {
82 @@ -136,8 +138,14 @@ struct phylink_mac_ops {
83 * based on @state->advertising and/or @state->speed and update
84 * @state->interface accordingly. See phylink_helper_basex_speed().
86 - * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink expects the
87 - * MAC driver to return all supported link modes.
88 + * When @config->supported_interfaces has been set, phylink will iterate
89 + * over the supported interfaces to determine the full capability of the
90 + * MAC. The validation function must not print errors if @state->interface
91 + * is set to an unexpected value.
93 + * When @config->supported_interfaces is empty, phylink will call this
94 + * function with @state->interface set to %PHY_INTERFACE_MODE_NA, and
95 + * expects the MAC driver to return all supported link modes.
97 * If the @state->interface mode is not supported, then the @supported
98 * mask must be cleared.