9800884f6e9a1e5efd4a941b8e1d98300c176ac2
[openwrt/staging/jogo.git] /
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
5 validation
6
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:
10
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.)
14
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>
18 ---
19 drivers/net/phy/phylink.c | 36 ++++++++++++++++++++++++++++++++++++
20 include/linux/phylink.h | 12 ++++++++++--
21 2 files changed, 46 insertions(+), 2 deletions(-)
22
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";
27 }
28
29 +static int phylink_validate_any(struct phylink *pl, unsigned long *supported,
30 + struct phylink_link_state *state)
31 +{
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;
36 + int intf;
37 +
38 + for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) {
39 + if (test_bit(intf, pl->config->supported_interfaces)) {
40 + linkmode_copy(s, supported);
41 +
42 + t = *state;
43 + t.interface = intf;
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);
47 + }
48 + }
49 +
50 + linkmode_copy(supported, all_s);
51 + linkmode_copy(state->advertising, all_adv);
52 +
53 + return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
54 +}
55 +
56 static int phylink_validate(struct phylink *pl, unsigned long *supported,
57 struct phylink_link_state *state)
58 {
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);
62 +
63 + if (!test_bit(state->interface,
64 + pl->config->supported_interfaces))
65 + return -EINVAL;
66 + }
67 +
68 pl->mac_ops->validate(pl->config, supported, state);
69
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.
79 */
80 struct phylink_config {
81 struct device *dev;
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().
85 *
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.
92 + *
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.
96 *
97 * If the @state->interface mode is not supported, then the @supported
98 * mask must be cleared.