Andrew Lunn | 5f85757 | 2019-01-21 19:10:19 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2 | /* |
| 3 | * phylink models the MAC to optional PHY connection, supporting |
| 4 | * technologies such as SFP cages where the PHY is hot-pluggable. |
| 5 | * |
| 6 | * Copyright (C) 2015 Russell King |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/ethtool.h> |
| 9 | #include <linux/export.h> |
| 10 | #include <linux/gpio/consumer.h> |
| 11 | #include <linux/netdevice.h> |
| 12 | #include <linux/of.h> |
| 13 | #include <linux/of_mdio.h> |
| 14 | #include <linux/phy.h> |
| 15 | #include <linux/phy_fixed.h> |
| 16 | #include <linux/phylink.h> |
| 17 | #include <linux/rtnetlink.h> |
| 18 | #include <linux/spinlock.h> |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 19 | #include <linux/timer.h> |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 20 | #include <linux/workqueue.h> |
| 21 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 22 | #include "sfp.h" |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 23 | #include "swphy.h" |
| 24 | |
| 25 | #define SUPPORTED_INTERFACES \ |
| 26 | (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \ |
| 27 | SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane) |
| 28 | #define ADVERTISED_INTERFACES \ |
| 29 | (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \ |
| 30 | ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane) |
| 31 | |
| 32 | enum { |
| 33 | PHYLINK_DISABLE_STOPPED, |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 34 | PHYLINK_DISABLE_LINK, |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 37 | /** |
| 38 | * struct phylink - internal data type for phylink |
| 39 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 40 | struct phylink { |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 41 | /* private: */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 42 | struct net_device *netdev; |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 43 | const struct phylink_mac_ops *mac_ops; |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 44 | const struct phylink_pcs_ops *pcs_ops; |
Ioana Ciornei | 44cc27e | 2019-05-28 20:38:12 +0300 | [diff] [blame] | 45 | struct phylink_config *config; |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 46 | struct device *dev; |
| 47 | unsigned int old_link_state:1; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 48 | |
| 49 | unsigned long phylink_disable_state; /* bitmask of disables */ |
| 50 | struct phy_device *phydev; |
| 51 | phy_interface_t link_interface; /* PHY_INTERFACE_xxx */ |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 52 | u8 cfg_link_an_mode; /* MLO_AN_xxx */ |
| 53 | u8 cur_link_an_mode; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 54 | u8 link_port; /* The current non-phy ethtool port */ |
| 55 | __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); |
| 56 | |
| 57 | /* The link configuration settings */ |
| 58 | struct phylink_link_state link_config; |
Russell King | c678726 | 2019-05-28 10:27:21 +0100 | [diff] [blame] | 59 | |
| 60 | /* The current settings */ |
| 61 | phy_interface_t cur_interface; |
| 62 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 63 | struct gpio_desc *link_gpio; |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 64 | unsigned int link_irq; |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 65 | struct timer_list link_poll; |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 66 | void (*get_fixed_state)(struct net_device *dev, |
| 67 | struct phylink_link_state *s); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 68 | |
| 69 | struct mutex state_mutex; |
| 70 | struct phylink_link_state phy_state; |
| 71 | struct work_struct resolve; |
| 72 | |
| 73 | bool mac_link_dropped; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 74 | |
| 75 | struct sfp_bus *sfp_bus; |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 76 | bool sfp_may_have_phy; |
| 77 | __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support); |
| 78 | u8 sfp_port; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 81 | #define phylink_printk(level, pl, fmt, ...) \ |
| 82 | do { \ |
| 83 | if ((pl)->config->type == PHYLINK_NETDEV) \ |
| 84 | netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \ |
| 85 | else if ((pl)->config->type == PHYLINK_DEV) \ |
| 86 | dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \ |
| 87 | } while (0) |
| 88 | |
| 89 | #define phylink_err(pl, fmt, ...) \ |
| 90 | phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__) |
| 91 | #define phylink_warn(pl, fmt, ...) \ |
| 92 | phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__) |
| 93 | #define phylink_info(pl, fmt, ...) \ |
| 94 | phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__) |
Florian Fainelli | 9d68db5 | 2019-10-31 15:42:26 -0700 | [diff] [blame] | 95 | #if defined(CONFIG_DYNAMIC_DEBUG) |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 96 | #define phylink_dbg(pl, fmt, ...) \ |
Florian Fainelli | 9d68db5 | 2019-10-31 15:42:26 -0700 | [diff] [blame] | 97 | do { \ |
| 98 | if ((pl)->config->type == PHYLINK_NETDEV) \ |
| 99 | netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \ |
| 100 | else if ((pl)->config->type == PHYLINK_DEV) \ |
| 101 | dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \ |
| 102 | } while (0) |
| 103 | #elif defined(DEBUG) |
| 104 | #define phylink_dbg(pl, fmt, ...) \ |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 105 | phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__) |
Florian Fainelli | 9d68db5 | 2019-10-31 15:42:26 -0700 | [diff] [blame] | 106 | #else |
| 107 | #define phylink_dbg(pl, fmt, ...) \ |
| 108 | ({ \ |
| 109 | if (0) \ |
| 110 | phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \ |
| 111 | }) |
| 112 | #endif |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 113 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 114 | /** |
| 115 | * phylink_set_port_modes() - set the port type modes in the ethtool mask |
| 116 | * @mask: ethtool link mode mask |
| 117 | * |
| 118 | * Sets all the port type modes in the ethtool mask. MAC drivers should |
| 119 | * use this in their 'validate' callback. |
| 120 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 121 | void phylink_set_port_modes(unsigned long *mask) |
| 122 | { |
| 123 | phylink_set(mask, TP); |
| 124 | phylink_set(mask, AUI); |
| 125 | phylink_set(mask, MII); |
| 126 | phylink_set(mask, FIBRE); |
| 127 | phylink_set(mask, BNC); |
| 128 | phylink_set(mask, Backplane); |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(phylink_set_port_modes); |
| 131 | |
| 132 | static int phylink_is_empty_linkmode(const unsigned long *linkmode) |
| 133 | { |
| 134 | __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, }; |
| 135 | |
| 136 | phylink_set_port_modes(tmp); |
| 137 | phylink_set(tmp, Autoneg); |
| 138 | phylink_set(tmp, Pause); |
| 139 | phylink_set(tmp, Asym_Pause); |
| 140 | |
Russell King | 554032c | 2019-10-15 11:28:46 +0100 | [diff] [blame] | 141 | return linkmode_subset(linkmode, tmp); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static const char *phylink_an_mode_str(unsigned int mode) |
| 145 | { |
| 146 | static const char *modestr[] = { |
| 147 | [MLO_AN_PHY] = "phy", |
| 148 | [MLO_AN_FIXED] = "fixed", |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 149 | [MLO_AN_INBAND] = "inband", |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown"; |
| 153 | } |
| 154 | |
| 155 | static int phylink_validate(struct phylink *pl, unsigned long *supported, |
| 156 | struct phylink_link_state *state) |
| 157 | { |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 158 | pl->mac_ops->validate(pl->config, supported, state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 159 | |
| 160 | return phylink_is_empty_linkmode(supported) ? -EINVAL : 0; |
| 161 | } |
| 162 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 163 | static int phylink_parse_fixedlink(struct phylink *pl, |
| 164 | struct fwnode_handle *fwnode) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 165 | { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 166 | struct fwnode_handle *fixed_node; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 167 | const struct phy_setting *s; |
| 168 | struct gpio_desc *desc; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 169 | u32 speed; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 170 | int ret; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 171 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 172 | fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 173 | if (fixed_node) { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 174 | ret = fwnode_property_read_u32(fixed_node, "speed", &speed); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 175 | |
| 176 | pl->link_config.speed = speed; |
| 177 | pl->link_config.duplex = DUPLEX_HALF; |
| 178 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 179 | if (fwnode_property_read_bool(fixed_node, "full-duplex")) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 180 | pl->link_config.duplex = DUPLEX_FULL; |
| 181 | |
| 182 | /* We treat the "pause" and "asym-pause" terminology as |
| 183 | * defining the link partner's ability. */ |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 184 | if (fwnode_property_read_bool(fixed_node, "pause")) |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 185 | __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 186 | pl->link_config.lp_advertising); |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 187 | if (fwnode_property_read_bool(fixed_node, "asym-pause")) |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 188 | __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 189 | pl->link_config.lp_advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 190 | |
| 191 | if (ret == 0) { |
Dmitry Torokhov | b605c9a | 2020-01-02 17:03:18 -0800 | [diff] [blame] | 192 | desc = fwnode_gpiod_get_index(fixed_node, "link", 0, |
| 193 | GPIOD_IN, "?"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 194 | |
| 195 | if (!IS_ERR(desc)) |
| 196 | pl->link_gpio = desc; |
| 197 | else if (desc == ERR_PTR(-EPROBE_DEFER)) |
| 198 | ret = -EPROBE_DEFER; |
| 199 | } |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 200 | fwnode_handle_put(fixed_node); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 201 | |
| 202 | if (ret) |
| 203 | return ret; |
| 204 | } else { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 205 | u32 prop[5]; |
| 206 | |
| 207 | ret = fwnode_property_read_u32_array(fwnode, "fixed-link", |
| 208 | NULL, 0); |
| 209 | if (ret != ARRAY_SIZE(prop)) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 210 | phylink_err(pl, "broken fixed-link?\n"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | } |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 213 | |
| 214 | ret = fwnode_property_read_u32_array(fwnode, "fixed-link", |
| 215 | prop, ARRAY_SIZE(prop)); |
| 216 | if (!ret) { |
| 217 | pl->link_config.duplex = prop[1] ? |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 218 | DUPLEX_FULL : DUPLEX_HALF; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 219 | pl->link_config.speed = prop[2]; |
| 220 | if (prop[3]) |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 221 | __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 222 | pl->link_config.lp_advertising); |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 223 | if (prop[4]) |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 224 | __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 225 | pl->link_config.lp_advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | if (pl->link_config.speed > SPEED_1000 && |
| 230 | pl->link_config.duplex != DUPLEX_FULL) |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 231 | phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n", |
| 232 | pl->link_config.speed); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 233 | |
| 234 | bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 235 | linkmode_copy(pl->link_config.advertising, pl->supported); |
| 236 | phylink_validate(pl, pl->supported, &pl->link_config); |
| 237 | |
| 238 | s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex, |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 239 | pl->supported, true); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 240 | linkmode_zero(pl->supported); |
| 241 | phylink_set(pl->supported, MII); |
René van Dorst | 8aace4f | 2019-07-27 11:40:11 +0200 | [diff] [blame] | 242 | phylink_set(pl->supported, Pause); |
| 243 | phylink_set(pl->supported, Asym_Pause); |
Russell King | 1ceb7ee | 2020-07-21 12:03:45 +0100 | [diff] [blame] | 244 | phylink_set(pl->supported, Autoneg); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 245 | if (s) { |
| 246 | __set_bit(s->bit, pl->supported); |
Russell King | 1ceb7ee | 2020-07-21 12:03:45 +0100 | [diff] [blame] | 247 | __set_bit(s->bit, pl->link_config.lp_advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 248 | } else { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 249 | phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n", |
| 250 | pl->link_config.duplex == DUPLEX_FULL ? "full" : "half", |
| 251 | pl->link_config.speed); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | linkmode_and(pl->link_config.advertising, pl->link_config.advertising, |
| 255 | pl->supported); |
| 256 | |
| 257 | pl->link_config.link = 1; |
| 258 | pl->link_config.an_complete = 1; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 263 | static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 264 | { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 265 | struct fwnode_handle *dn; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 266 | const char *managed; |
| 267 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 268 | dn = fwnode_get_named_child_node(fwnode, "fixed-link"); |
| 269 | if (dn || fwnode_property_present(fwnode, "fixed-link")) |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 270 | pl->cfg_link_an_mode = MLO_AN_FIXED; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 271 | fwnode_handle_put(dn); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 272 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 273 | if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 && |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 274 | strcmp(managed, "in-band-status") == 0) { |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 275 | if (pl->cfg_link_an_mode == MLO_AN_FIXED) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 276 | phylink_err(pl, |
| 277 | "can't use both fixed-link and in-band-status\n"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 278 | return -EINVAL; |
| 279 | } |
| 280 | |
| 281 | linkmode_zero(pl->supported); |
| 282 | phylink_set(pl->supported, MII); |
| 283 | phylink_set(pl->supported, Autoneg); |
| 284 | phylink_set(pl->supported, Asym_Pause); |
| 285 | phylink_set(pl->supported, Pause); |
| 286 | pl->link_config.an_enabled = true; |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 287 | pl->cfg_link_an_mode = MLO_AN_INBAND; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 288 | |
| 289 | switch (pl->link_config.interface) { |
| 290 | case PHY_INTERFACE_MODE_SGMII: |
Vladimir Oltean | 3a68ba6 | 2020-01-06 03:34:10 +0200 | [diff] [blame] | 291 | case PHY_INTERFACE_MODE_QSGMII: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 292 | phylink_set(pl->supported, 10baseT_Half); |
| 293 | phylink_set(pl->supported, 10baseT_Full); |
| 294 | phylink_set(pl->supported, 100baseT_Half); |
| 295 | phylink_set(pl->supported, 100baseT_Full); |
| 296 | phylink_set(pl->supported, 1000baseT_Half); |
| 297 | phylink_set(pl->supported, 1000baseT_Full); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 298 | break; |
| 299 | |
| 300 | case PHY_INTERFACE_MODE_1000BASEX: |
| 301 | phylink_set(pl->supported, 1000baseX_Full); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 302 | break; |
| 303 | |
| 304 | case PHY_INTERFACE_MODE_2500BASEX: |
| 305 | phylink_set(pl->supported, 2500baseX_Full); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 306 | break; |
| 307 | |
Alex Marginean | 04e2246 | 2020-01-18 14:19:15 +0200 | [diff] [blame] | 308 | case PHY_INTERFACE_MODE_USXGMII: |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 309 | case PHY_INTERFACE_MODE_10GKR: |
Russell King | e0f909b | 2020-01-03 20:43:23 +0000 | [diff] [blame] | 310 | case PHY_INTERFACE_MODE_10GBASER: |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 311 | phylink_set(pl->supported, 10baseT_Half); |
| 312 | phylink_set(pl->supported, 10baseT_Full); |
| 313 | phylink_set(pl->supported, 100baseT_Half); |
| 314 | phylink_set(pl->supported, 100baseT_Full); |
| 315 | phylink_set(pl->supported, 1000baseT_Half); |
| 316 | phylink_set(pl->supported, 1000baseT_Full); |
| 317 | phylink_set(pl->supported, 1000baseX_Full); |
Jose Abreu | c580165 | 2020-03-09 09:36:24 +0100 | [diff] [blame] | 318 | phylink_set(pl->supported, 1000baseKX_Full); |
Vladimir Oltean | 6cbdcf2 | 2020-01-16 19:36:56 +0200 | [diff] [blame] | 319 | phylink_set(pl->supported, 2500baseT_Full); |
| 320 | phylink_set(pl->supported, 2500baseX_Full); |
| 321 | phylink_set(pl->supported, 5000baseT_Full); |
| 322 | phylink_set(pl->supported, 10000baseT_Full); |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 323 | phylink_set(pl->supported, 10000baseKR_Full); |
Jose Abreu | c580165 | 2020-03-09 09:36:24 +0100 | [diff] [blame] | 324 | phylink_set(pl->supported, 10000baseKX4_Full); |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 325 | phylink_set(pl->supported, 10000baseCR_Full); |
| 326 | phylink_set(pl->supported, 10000baseSR_Full); |
| 327 | phylink_set(pl->supported, 10000baseLR_Full); |
| 328 | phylink_set(pl->supported, 10000baseLRM_Full); |
| 329 | phylink_set(pl->supported, 10000baseER_Full); |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 330 | break; |
| 331 | |
Jose Abreu | 1671c42 | 2020-03-12 18:10:10 +0100 | [diff] [blame] | 332 | case PHY_INTERFACE_MODE_XLGMII: |
| 333 | phylink_set(pl->supported, 25000baseCR_Full); |
| 334 | phylink_set(pl->supported, 25000baseKR_Full); |
| 335 | phylink_set(pl->supported, 25000baseSR_Full); |
| 336 | phylink_set(pl->supported, 40000baseKR4_Full); |
| 337 | phylink_set(pl->supported, 40000baseCR4_Full); |
| 338 | phylink_set(pl->supported, 40000baseSR4_Full); |
| 339 | phylink_set(pl->supported, 40000baseLR4_Full); |
| 340 | phylink_set(pl->supported, 50000baseCR2_Full); |
| 341 | phylink_set(pl->supported, 50000baseKR2_Full); |
| 342 | phylink_set(pl->supported, 50000baseSR2_Full); |
| 343 | phylink_set(pl->supported, 50000baseKR_Full); |
| 344 | phylink_set(pl->supported, 50000baseSR_Full); |
| 345 | phylink_set(pl->supported, 50000baseCR_Full); |
| 346 | phylink_set(pl->supported, 50000baseLR_ER_FR_Full); |
| 347 | phylink_set(pl->supported, 50000baseDR_Full); |
| 348 | phylink_set(pl->supported, 100000baseKR4_Full); |
| 349 | phylink_set(pl->supported, 100000baseSR4_Full); |
| 350 | phylink_set(pl->supported, 100000baseCR4_Full); |
| 351 | phylink_set(pl->supported, 100000baseLR4_ER4_Full); |
| 352 | phylink_set(pl->supported, 100000baseKR2_Full); |
| 353 | phylink_set(pl->supported, 100000baseSR2_Full); |
| 354 | phylink_set(pl->supported, 100000baseCR2_Full); |
| 355 | phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full); |
| 356 | phylink_set(pl->supported, 100000baseDR2_Full); |
| 357 | break; |
| 358 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 359 | default: |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 360 | phylink_err(pl, |
| 361 | "incorrect link mode %s for in-band status\n", |
| 362 | phy_modes(pl->link_config.interface)); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 363 | return -EINVAL; |
| 364 | } |
| 365 | |
| 366 | linkmode_copy(pl->link_config.advertising, pl->supported); |
| 367 | |
| 368 | if (phylink_validate(pl, pl->supported, &pl->link_config)) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 369 | phylink_err(pl, |
| 370 | "failed to validate link configuration for in-band status\n"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 371 | return -EINVAL; |
| 372 | } |
Jose Abreu | 9414819 | 2020-03-09 09:36:25 +0100 | [diff] [blame] | 373 | |
| 374 | /* Check if MAC/PCS also supports Autoneg. */ |
| 375 | pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
Russell King | 2d5fbef | 2020-02-15 15:49:43 +0000 | [diff] [blame] | 381 | static void phylink_apply_manual_flow(struct phylink *pl, |
| 382 | struct phylink_link_state *state) |
| 383 | { |
| 384 | /* If autoneg is disabled, pause AN is also disabled */ |
| 385 | if (!state->an_enabled) |
| 386 | state->pause &= ~MLO_PAUSE_AN; |
| 387 | |
| 388 | /* Manual configuration of pause modes */ |
| 389 | if (!(pl->link_config.pause & MLO_PAUSE_AN)) |
| 390 | state->pause = pl->link_config.pause; |
| 391 | } |
| 392 | |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 393 | static void phylink_resolve_flow(struct phylink_link_state *state) |
| 394 | { |
| 395 | bool tx_pause, rx_pause; |
| 396 | |
| 397 | state->pause = MLO_PAUSE_NONE; |
| 398 | if (state->duplex == DUPLEX_FULL) { |
| 399 | linkmode_resolve_pause(state->advertising, |
| 400 | state->lp_advertising, |
| 401 | &tx_pause, &rx_pause); |
| 402 | if (tx_pause) |
| 403 | state->pause |= MLO_PAUSE_TX; |
| 404 | if (rx_pause) |
| 405 | state->pause |= MLO_PAUSE_RX; |
| 406 | } |
| 407 | } |
| 408 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 409 | static void phylink_mac_config(struct phylink *pl, |
| 410 | const struct phylink_link_state *state) |
| 411 | { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 412 | phylink_dbg(pl, |
| 413 | "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n", |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 414 | __func__, phylink_an_mode_str(pl->cur_link_an_mode), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 415 | phy_modes(state->interface), |
| 416 | phy_speed_to_str(state->speed), |
| 417 | phy_duplex_to_str(state->duplex), |
| 418 | __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising, |
| 419 | state->pause, state->link, state->an_enabled); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 420 | |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 421 | pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 424 | static void phylink_mac_pcs_an_restart(struct phylink *pl) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 425 | { |
| 426 | if (pl->link_config.an_enabled && |
Russell King | 575691b | 2020-06-24 12:30:04 +0100 | [diff] [blame] | 427 | phy_interface_mode_is_8023z(pl->link_config.interface) && |
| 428 | phylink_autoneg_inband(pl->cur_link_an_mode)) { |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 429 | if (pl->pcs_ops) |
| 430 | pl->pcs_ops->pcs_an_restart(pl->config); |
| 431 | else |
| 432 | pl->mac_ops->mac_an_restart(pl->config); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | static void phylink_pcs_config(struct phylink *pl, bool force_restart, |
| 437 | const struct phylink_link_state *state) |
| 438 | { |
| 439 | bool restart = force_restart; |
| 440 | |
| 441 | if (pl->pcs_ops && pl->pcs_ops->pcs_config(pl->config, |
| 442 | pl->cur_link_an_mode, |
| 443 | state->interface, |
| 444 | state->advertising)) |
| 445 | restart = true; |
| 446 | |
| 447 | phylink_mac_config(pl, state); |
| 448 | |
| 449 | if (restart) |
| 450 | phylink_mac_pcs_an_restart(pl); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 453 | static void phylink_mac_pcs_get_state(struct phylink *pl, |
| 454 | struct phylink_link_state *state) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 455 | { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 456 | linkmode_copy(state->advertising, pl->link_config.advertising); |
| 457 | linkmode_zero(state->lp_advertising); |
| 458 | state->interface = pl->link_config.interface; |
| 459 | state->an_enabled = pl->link_config.an_enabled; |
Heiner Kallweit | d25ed41 | 2019-02-26 19:29:22 +0100 | [diff] [blame] | 460 | state->speed = SPEED_UNKNOWN; |
| 461 | state->duplex = DUPLEX_UNKNOWN; |
| 462 | state->pause = MLO_PAUSE_NONE; |
| 463 | state->an_complete = 0; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 464 | state->link = 1; |
| 465 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 466 | if (pl->pcs_ops) |
| 467 | pl->pcs_ops->pcs_get_state(pl->config, state); |
| 468 | else |
| 469 | pl->mac_ops->mac_pcs_get_state(pl->config, state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* The fixed state is... fixed except for the link state, |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 473 | * which may be determined by a GPIO or a callback. |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 474 | */ |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 475 | static void phylink_get_fixed_state(struct phylink *pl, |
| 476 | struct phylink_link_state *state) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 477 | { |
| 478 | *state = pl->link_config; |
Russell King | 5c05c1d | 2020-04-23 17:02:56 +0100 | [diff] [blame] | 479 | if (pl->config->get_fixed_state) |
| 480 | pl->config->get_fixed_state(pl->config, state); |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 481 | else if (pl->link_gpio) |
Florian Fainelli | bb322a9 | 2018-05-10 13:17:29 -0700 | [diff] [blame] | 482 | state->link = !!gpiod_get_value_cansleep(pl->link_gpio); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 483 | |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 484 | phylink_resolve_flow(state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 487 | static void phylink_mac_initial_config(struct phylink *pl, bool force_restart) |
Russell King | 97fec51 | 2020-02-15 15:50:03 +0000 | [diff] [blame] | 488 | { |
| 489 | struct phylink_link_state link_state; |
| 490 | |
| 491 | switch (pl->cur_link_an_mode) { |
| 492 | case MLO_AN_PHY: |
| 493 | link_state = pl->phy_state; |
| 494 | break; |
| 495 | |
| 496 | case MLO_AN_FIXED: |
| 497 | phylink_get_fixed_state(pl, &link_state); |
| 498 | break; |
| 499 | |
| 500 | case MLO_AN_INBAND: |
| 501 | link_state = pl->link_config; |
| 502 | if (link_state.interface == PHY_INTERFACE_MODE_SGMII) |
| 503 | link_state.pause = MLO_PAUSE_NONE; |
| 504 | break; |
| 505 | |
| 506 | default: /* can't happen */ |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | link_state.link = false; |
| 511 | |
| 512 | phylink_apply_manual_flow(pl, &link_state); |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 513 | phylink_pcs_config(pl, force_restart, &link_state); |
Russell King | 97fec51 | 2020-02-15 15:50:03 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 516 | static const char *phylink_pause_to_str(int pause) |
| 517 | { |
| 518 | switch (pause & MLO_PAUSE_TXRX_MASK) { |
| 519 | case MLO_PAUSE_TX | MLO_PAUSE_RX: |
| 520 | return "rx/tx"; |
| 521 | case MLO_PAUSE_TX: |
| 522 | return "tx"; |
| 523 | case MLO_PAUSE_RX: |
| 524 | return "rx"; |
| 525 | default: |
| 526 | return "off"; |
| 527 | } |
| 528 | } |
| 529 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 530 | static void phylink_link_up(struct phylink *pl, |
| 531 | struct phylink_link_state link_state) |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 532 | { |
| 533 | struct net_device *ndev = pl->netdev; |
| 534 | |
David S. Miller | b4b12b0 | 2019-05-31 10:49:43 -0700 | [diff] [blame] | 535 | pl->cur_interface = link_state.interface; |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 536 | |
| 537 | if (pl->pcs_ops && pl->pcs_ops->pcs_link_up) |
| 538 | pl->pcs_ops->pcs_link_up(pl->config, pl->cur_link_an_mode, |
| 539 | pl->cur_interface, |
| 540 | link_state.speed, link_state.duplex); |
| 541 | |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 542 | pl->mac_ops->mac_link_up(pl->config, pl->phydev, |
| 543 | pl->cur_link_an_mode, pl->cur_interface, |
| 544 | link_state.speed, link_state.duplex, |
| 545 | !!(link_state.pause & MLO_PAUSE_TX), |
| 546 | !!(link_state.pause & MLO_PAUSE_RX)); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 547 | |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 548 | if (ndev) |
| 549 | netif_carrier_on(ndev); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 550 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 551 | phylink_info(pl, |
| 552 | "Link is Up - %s/%s - flow control %s\n", |
| 553 | phy_speed_to_str(link_state.speed), |
| 554 | phy_duplex_to_str(link_state.duplex), |
| 555 | phylink_pause_to_str(link_state.pause)); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 556 | } |
| 557 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 558 | static void phylink_link_down(struct phylink *pl) |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 559 | { |
| 560 | struct net_device *ndev = pl->netdev; |
| 561 | |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 562 | if (ndev) |
| 563 | netif_carrier_off(ndev); |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 564 | pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode, |
| 565 | pl->cur_interface); |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 566 | phylink_info(pl, "Link is Down\n"); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 567 | } |
| 568 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 569 | static void phylink_resolve(struct work_struct *w) |
| 570 | { |
| 571 | struct phylink *pl = container_of(w, struct phylink, resolve); |
| 572 | struct phylink_link_state link_state; |
| 573 | struct net_device *ndev = pl->netdev; |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 574 | bool mac_config = false; |
Russell King | b06e5ca | 2020-07-21 12:03:50 +0100 | [diff] [blame] | 575 | bool cur_link_state; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 576 | |
| 577 | mutex_lock(&pl->state_mutex); |
Russell King | b06e5ca | 2020-07-21 12:03:50 +0100 | [diff] [blame] | 578 | if (pl->netdev) |
| 579 | cur_link_state = netif_carrier_ok(ndev); |
| 580 | else |
| 581 | cur_link_state = pl->old_link_state; |
| 582 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 583 | if (pl->phylink_disable_state) { |
| 584 | pl->mac_link_dropped = false; |
| 585 | link_state.link = false; |
| 586 | } else if (pl->mac_link_dropped) { |
| 587 | link_state.link = false; |
| 588 | } else { |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 589 | switch (pl->cur_link_an_mode) { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 590 | case MLO_AN_PHY: |
| 591 | link_state = pl->phy_state; |
Russell King | 2d5fbef | 2020-02-15 15:49:43 +0000 | [diff] [blame] | 592 | phylink_apply_manual_flow(pl, &link_state); |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 593 | mac_config = link_state.link; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 594 | break; |
| 595 | |
| 596 | case MLO_AN_FIXED: |
| 597 | phylink_get_fixed_state(pl, &link_state); |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 598 | mac_config = link_state.link; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 599 | break; |
| 600 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 601 | case MLO_AN_INBAND: |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 602 | phylink_mac_pcs_get_state(pl, &link_state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 603 | |
Russell King | 406cb0c | 2019-05-20 16:07:20 +0100 | [diff] [blame] | 604 | /* If we have a phy, the "up" state is the union of |
| 605 | * both the PHY and the MAC */ |
| 606 | if (pl->phydev) |
| 607 | link_state.link &= pl->phy_state.link; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 608 | |
Russell King | 406cb0c | 2019-05-20 16:07:20 +0100 | [diff] [blame] | 609 | /* Only update if the PHY link is up */ |
| 610 | if (pl->phydev && pl->phy_state.link) { |
| 611 | link_state.interface = pl->phy_state.interface; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 612 | |
Russell King | 406cb0c | 2019-05-20 16:07:20 +0100 | [diff] [blame] | 613 | /* If we have a PHY, we need to update with |
Russell King | 33faac8 | 2020-02-15 15:49:48 +0000 | [diff] [blame] | 614 | * the PHY flow control bits. */ |
| 615 | link_state.pause = pl->phy_state.pause; |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 616 | mac_config = true; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 617 | } |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 618 | phylink_apply_manual_flow(pl, &link_state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 619 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Russell King | 16319a7 | 2020-07-21 12:04:00 +0100 | [diff] [blame] | 623 | if (mac_config) { |
| 624 | if (link_state.interface != pl->link_config.interface) { |
| 625 | /* The interface has changed, force the link down and |
| 626 | * then reconfigure. |
| 627 | */ |
| 628 | if (cur_link_state) { |
| 629 | phylink_link_down(pl); |
| 630 | cur_link_state = false; |
| 631 | } |
Russell King | 5005b16 | 2020-07-21 12:04:05 +0100 | [diff] [blame] | 632 | phylink_pcs_config(pl, false, &link_state); |
| 633 | pl->link_config.interface = link_state.interface; |
Russell King | 7cceb59 | 2020-07-21 12:04:10 +0100 | [diff] [blame] | 634 | } else if (!pl->pcs_ops) { |
Russell King | 5005b16 | 2020-07-21 12:04:05 +0100 | [diff] [blame] | 635 | /* The interface remains unchanged, only the speed, |
| 636 | * duplex or pause settings have changed. Call the |
Russell King | 7cceb59 | 2020-07-21 12:04:10 +0100 | [diff] [blame] | 637 | * old mac_config() method to configure the MAC/PCS |
| 638 | * only if we do not have a PCS installed (an |
| 639 | * unconverted user.) |
Russell King | 5005b16 | 2020-07-21 12:04:05 +0100 | [diff] [blame] | 640 | */ |
| 641 | phylink_mac_config(pl, &link_state); |
Russell King | 16319a7 | 2020-07-21 12:04:00 +0100 | [diff] [blame] | 642 | } |
Russell King | 16319a7 | 2020-07-21 12:04:00 +0100 | [diff] [blame] | 643 | } |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 644 | |
Russell King | b06e5ca | 2020-07-21 12:03:50 +0100 | [diff] [blame] | 645 | if (link_state.link != cur_link_state) { |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 646 | pl->old_link_state = link_state.link; |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 647 | if (!link_state.link) |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 648 | phylink_link_down(pl); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 649 | else |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 650 | phylink_link_up(pl, link_state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 651 | } |
| 652 | if (!link_state.link && pl->mac_link_dropped) { |
| 653 | pl->mac_link_dropped = false; |
| 654 | queue_work(system_power_efficient_wq, &pl->resolve); |
| 655 | } |
| 656 | mutex_unlock(&pl->state_mutex); |
| 657 | } |
| 658 | |
| 659 | static void phylink_run_resolve(struct phylink *pl) |
| 660 | { |
| 661 | if (!pl->phylink_disable_state) |
| 662 | queue_work(system_power_efficient_wq, &pl->resolve); |
| 663 | } |
| 664 | |
Russell King | 87454b6 | 2019-02-11 15:04:24 +0000 | [diff] [blame] | 665 | static void phylink_run_resolve_and_disable(struct phylink *pl, int bit) |
| 666 | { |
| 667 | unsigned long state = pl->phylink_disable_state; |
| 668 | |
| 669 | set_bit(bit, &pl->phylink_disable_state); |
| 670 | if (state == 0) { |
| 671 | queue_work(system_power_efficient_wq, &pl->resolve); |
| 672 | flush_work(&pl->resolve); |
| 673 | } |
| 674 | } |
| 675 | |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 676 | static void phylink_fixed_poll(struct timer_list *t) |
| 677 | { |
| 678 | struct phylink *pl = container_of(t, struct phylink, link_poll); |
| 679 | |
| 680 | mod_timer(t, jiffies + HZ); |
| 681 | |
| 682 | phylink_run_resolve(pl); |
| 683 | } |
| 684 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 685 | static const struct sfp_upstream_ops sfp_phylink_ops; |
| 686 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 687 | static int phylink_register_sfp(struct phylink *pl, |
| 688 | struct fwnode_handle *fwnode) |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 689 | { |
Russell King | 2203cbf | 2019-10-15 11:38:39 +0100 | [diff] [blame] | 690 | struct sfp_bus *bus; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 691 | int ret; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 692 | |
Russell King | eed70fd | 2020-01-03 15:13:56 +0000 | [diff] [blame] | 693 | if (!fwnode) |
| 694 | return 0; |
| 695 | |
Russell King | 727b366 | 2019-11-08 17:39:29 +0000 | [diff] [blame] | 696 | bus = sfp_bus_find_fwnode(fwnode); |
Russell King | 2203cbf | 2019-10-15 11:38:39 +0100 | [diff] [blame] | 697 | if (IS_ERR(bus)) { |
| 698 | ret = PTR_ERR(bus); |
| 699 | phylink_err(pl, "unable to attach SFP bus: %d\n", ret); |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 700 | return ret; |
| 701 | } |
| 702 | |
Russell King | 2203cbf | 2019-10-15 11:38:39 +0100 | [diff] [blame] | 703 | pl->sfp_bus = bus; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 704 | |
Russell King | 727b366 | 2019-11-08 17:39:29 +0000 | [diff] [blame] | 705 | ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops); |
| 706 | sfp_bus_put(bus); |
| 707 | |
| 708 | return ret; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 709 | } |
| 710 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 711 | /** |
| 712 | * phylink_create() - create a phylink instance |
Randy Dunlap | 9db74e5 | 2019-10-08 08:39:04 -0700 | [diff] [blame] | 713 | * @config: a pointer to the target &struct phylink_config |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 714 | * @fwnode: a pointer to a &struct fwnode_handle describing the network |
| 715 | * interface |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 716 | * @iface: the desired link mode defined by &typedef phy_interface_t |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 717 | * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC. |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 718 | * |
| 719 | * Create a new phylink instance, and parse the link parameters found in @np. |
| 720 | * This will parse in-band modes, fixed-link or SFP configuration. |
| 721 | * |
Russell King | 269a6b5 | 2019-11-19 17:18:52 +0000 | [diff] [blame] | 722 | * Note: the rtnl lock must not be held when calling this function. |
| 723 | * |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 724 | * Returns a pointer to a &struct phylink, or an error-pointer value. Users |
| 725 | * must use IS_ERR() to check for errors from this function. |
| 726 | */ |
Ioana Ciornei | 44cc27e | 2019-05-28 20:38:12 +0300 | [diff] [blame] | 727 | struct phylink *phylink_create(struct phylink_config *config, |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 728 | struct fwnode_handle *fwnode, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 729 | phy_interface_t iface, |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 730 | const struct phylink_mac_ops *mac_ops) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 731 | { |
| 732 | struct phylink *pl; |
| 733 | int ret; |
| 734 | |
| 735 | pl = kzalloc(sizeof(*pl), GFP_KERNEL); |
| 736 | if (!pl) |
| 737 | return ERR_PTR(-ENOMEM); |
| 738 | |
| 739 | mutex_init(&pl->state_mutex); |
| 740 | INIT_WORK(&pl->resolve, phylink_resolve); |
Ioana Ciornei | 44cc27e | 2019-05-28 20:38:12 +0300 | [diff] [blame] | 741 | |
| 742 | pl->config = config; |
| 743 | if (config->type == PHYLINK_NETDEV) { |
| 744 | pl->netdev = to_net_dev(config->dev); |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 745 | } else if (config->type == PHYLINK_DEV) { |
| 746 | pl->dev = config->dev; |
Ioana Ciornei | 44cc27e | 2019-05-28 20:38:12 +0300 | [diff] [blame] | 747 | } else { |
| 748 | kfree(pl); |
| 749 | return ERR_PTR(-EINVAL); |
| 750 | } |
| 751 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 752 | pl->phy_state.interface = iface; |
| 753 | pl->link_interface = iface; |
Florian Fainelli | 4be11ef | 2017-12-12 16:00:29 -0800 | [diff] [blame] | 754 | if (iface == PHY_INTERFACE_MODE_MOCA) |
| 755 | pl->link_port = PORT_BNC; |
| 756 | else |
| 757 | pl->link_port = PORT_MII; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 758 | pl->link_config.interface = iface; |
| 759 | pl->link_config.pause = MLO_PAUSE_AN; |
| 760 | pl->link_config.speed = SPEED_UNKNOWN; |
| 761 | pl->link_config.duplex = DUPLEX_UNKNOWN; |
Russell King | 74ee0e8 | 2017-12-20 23:21:34 +0000 | [diff] [blame] | 762 | pl->link_config.an_enabled = true; |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 763 | pl->mac_ops = mac_ops; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 764 | __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 765 | timer_setup(&pl->link_poll, phylink_fixed_poll, 0); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 766 | |
| 767 | bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 768 | linkmode_copy(pl->link_config.advertising, pl->supported); |
| 769 | phylink_validate(pl, pl->supported, &pl->link_config); |
| 770 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 771 | ret = phylink_parse_mode(pl, fwnode); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 772 | if (ret < 0) { |
| 773 | kfree(pl); |
| 774 | return ERR_PTR(ret); |
| 775 | } |
| 776 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 777 | if (pl->cfg_link_an_mode == MLO_AN_FIXED) { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 778 | ret = phylink_parse_fixedlink(pl, fwnode); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 779 | if (ret < 0) { |
| 780 | kfree(pl); |
| 781 | return ERR_PTR(ret); |
| 782 | } |
| 783 | } |
| 784 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 785 | pl->cur_link_an_mode = pl->cfg_link_an_mode; |
| 786 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 787 | ret = phylink_register_sfp(pl, fwnode); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 788 | if (ret < 0) { |
| 789 | kfree(pl); |
| 790 | return ERR_PTR(ret); |
| 791 | } |
| 792 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 793 | return pl; |
| 794 | } |
| 795 | EXPORT_SYMBOL_GPL(phylink_create); |
| 796 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 797 | void phylink_add_pcs(struct phylink *pl, const struct phylink_pcs_ops *ops) |
| 798 | { |
| 799 | pl->pcs_ops = ops; |
| 800 | } |
| 801 | EXPORT_SYMBOL_GPL(phylink_add_pcs); |
| 802 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 803 | /** |
| 804 | * phylink_destroy() - cleanup and destroy the phylink instance |
| 805 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 806 | * |
| 807 | * Destroy a phylink instance. Any PHY that has been attached must have been |
| 808 | * cleaned up via phylink_disconnect_phy() prior to calling this function. |
Russell King | 269a6b5 | 2019-11-19 17:18:52 +0000 | [diff] [blame] | 809 | * |
| 810 | * Note: the rtnl lock must not be held when calling this function. |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 811 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 812 | void phylink_destroy(struct phylink *pl) |
| 813 | { |
Russell King | 727b366 | 2019-11-08 17:39:29 +0000 | [diff] [blame] | 814 | sfp_bus_del_upstream(pl->sfp_bus); |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 815 | if (pl->link_gpio) |
Florian Fainelli | daab334 | 2018-05-10 13:17:30 -0700 | [diff] [blame] | 816 | gpiod_put(pl->link_gpio); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 817 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 818 | cancel_work_sync(&pl->resolve); |
| 819 | kfree(pl); |
| 820 | } |
| 821 | EXPORT_SYMBOL_GPL(phylink_destroy); |
| 822 | |
Doug Berger | a307593 | 2020-05-18 15:23:59 -0700 | [diff] [blame] | 823 | static void phylink_phy_change(struct phy_device *phydev, bool up) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 824 | { |
| 825 | struct phylink *pl = phydev->phylink; |
Russell King | 33faac8 | 2020-02-15 15:49:48 +0000 | [diff] [blame] | 826 | bool tx_pause, rx_pause; |
| 827 | |
| 828 | phy_get_pause(phydev, &tx_pause, &rx_pause); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 829 | |
| 830 | mutex_lock(&pl->state_mutex); |
| 831 | pl->phy_state.speed = phydev->speed; |
| 832 | pl->phy_state.duplex = phydev->duplex; |
| 833 | pl->phy_state.pause = MLO_PAUSE_NONE; |
Russell King | 33faac8 | 2020-02-15 15:49:48 +0000 | [diff] [blame] | 834 | if (tx_pause) |
| 835 | pl->phy_state.pause |= MLO_PAUSE_TX; |
| 836 | if (rx_pause) |
| 837 | pl->phy_state.pause |= MLO_PAUSE_RX; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 838 | pl->phy_state.interface = phydev->interface; |
| 839 | pl->phy_state.link = up; |
| 840 | mutex_unlock(&pl->state_mutex); |
| 841 | |
| 842 | phylink_run_resolve(pl); |
| 843 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 844 | phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down", |
| 845 | phy_modes(phydev->interface), |
| 846 | phy_speed_to_str(phydev->speed), |
| 847 | phy_duplex_to_str(phydev->duplex)); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 848 | } |
| 849 | |
Russell King | e45d1f5 | 2019-12-11 10:56:30 +0000 | [diff] [blame] | 850 | static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy, |
| 851 | phy_interface_t interface) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 852 | { |
| 853 | struct phylink_link_state config; |
| 854 | __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); |
Florian Fainelli | e27f178 | 2020-01-12 09:35:38 -0800 | [diff] [blame] | 855 | char *irq_str; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 856 | int ret; |
| 857 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 858 | /* |
| 859 | * This is the new way of dealing with flow control for PHYs, |
| 860 | * as described by Timur Tabi in commit 529ed1275263 ("net: phy: |
| 861 | * phy drivers should not set SUPPORTED_[Asym_]Pause") except |
| 862 | * using our validate call to the MAC, we rely upon the MAC |
| 863 | * clearing the bits from both supported and advertising fields. |
| 864 | */ |
Russell King | 725ea4b | 2019-11-15 20:05:45 +0000 | [diff] [blame] | 865 | phy_support_asym_pause(phy); |
| 866 | |
| 867 | memset(&config, 0, sizeof(config)); |
| 868 | linkmode_copy(supported, phy->supported); |
| 869 | linkmode_copy(config.advertising, phy->advertising); |
Russell King | df3f57a | 2019-12-13 18:22:07 +0000 | [diff] [blame] | 870 | |
| 871 | /* Clause 45 PHYs switch their Serdes lane between several different |
| 872 | * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G |
| 873 | * speeds. We really need to know which interface modes the PHY and |
| 874 | * MAC supports to properly work out which linkmodes can be supported. |
| 875 | */ |
| 876 | if (phy->is_c45 && |
| 877 | interface != PHY_INTERFACE_MODE_RXAUI && |
| 878 | interface != PHY_INTERFACE_MODE_XAUI && |
| 879 | interface != PHY_INTERFACE_MODE_USXGMII) |
| 880 | config.interface = PHY_INTERFACE_MODE_NA; |
| 881 | else |
| 882 | config.interface = interface; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 883 | |
| 884 | ret = phylink_validate(pl, supported, &config); |
Hauke Mehrtens | 20d8bb0 | 2020-03-02 00:55:02 +0100 | [diff] [blame] | 885 | if (ret) { |
| 886 | phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n", |
| 887 | phy_modes(config.interface), |
| 888 | __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported, |
| 889 | __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising, |
| 890 | ret); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 891 | return ret; |
Hauke Mehrtens | 20d8bb0 | 2020-03-02 00:55:02 +0100 | [diff] [blame] | 892 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 893 | |
| 894 | phy->phylink = pl; |
| 895 | phy->phy_link_change = phylink_phy_change; |
| 896 | |
Florian Fainelli | e27f178 | 2020-01-12 09:35:38 -0800 | [diff] [blame] | 897 | irq_str = phy_attached_info_irq(phy); |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 898 | phylink_info(pl, |
Florian Fainelli | e27f178 | 2020-01-12 09:35:38 -0800 | [diff] [blame] | 899 | "PHY [%s] driver [%s] (irq=%s)\n", |
| 900 | dev_name(&phy->mdio.dev), phy->drv->name, irq_str); |
| 901 | kfree(irq_str); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 902 | |
| 903 | mutex_lock(&phy->lock); |
| 904 | mutex_lock(&pl->state_mutex); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 905 | pl->phydev = phy; |
Russell King | e45d1f5 | 2019-12-11 10:56:30 +0000 | [diff] [blame] | 906 | pl->phy_state.interface = interface; |
Russell King | 97fec51 | 2020-02-15 15:50:03 +0000 | [diff] [blame] | 907 | pl->phy_state.pause = MLO_PAUSE_NONE; |
| 908 | pl->phy_state.speed = SPEED_UNKNOWN; |
| 909 | pl->phy_state.duplex = DUPLEX_UNKNOWN; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 910 | linkmode_copy(pl->supported, supported); |
| 911 | linkmode_copy(pl->link_config.advertising, config.advertising); |
| 912 | |
Colin Ian King | cc1122b | 2018-03-01 10:23:03 +0000 | [diff] [blame] | 913 | /* Restrict the phy advertisement according to the MAC support. */ |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 914 | linkmode_copy(phy->advertising, config.advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 915 | mutex_unlock(&pl->state_mutex); |
| 916 | mutex_unlock(&phy->lock); |
| 917 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 918 | phylink_dbg(pl, |
| 919 | "phy: setting supported %*pb advertising %*pb\n", |
| 920 | __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported, |
| 921 | __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 922 | |
Heiner Kallweit | 434a431 | 2019-01-23 07:31:58 +0100 | [diff] [blame] | 923 | if (phy_interrupt_is_valid(phy)) |
| 924 | phy_request_interrupt(phy); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 929 | static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy, |
| 930 | phy_interface_t interface) |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 931 | { |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 932 | if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED || |
| 933 | (pl->cfg_link_an_mode == MLO_AN_INBAND && |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 934 | phy_interface_mode_is_8023z(interface)))) |
| 935 | return -EINVAL; |
| 936 | |
| 937 | if (pl->phydev) |
| 938 | return -EBUSY; |
| 939 | |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 940 | return phy_attach_direct(pl->netdev, phy, 0, interface); |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 941 | } |
| 942 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 943 | /** |
| 944 | * phylink_connect_phy() - connect a PHY to the phylink instance |
| 945 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 946 | * @phy: a pointer to a &struct phy_device. |
| 947 | * |
| 948 | * Connect @phy to the phylink instance specified by @pl by calling |
| 949 | * phy_attach_direct(). Configure the @phy according to the MAC driver's |
| 950 | * capabilities, start the PHYLIB state machine and enable any interrupts |
| 951 | * that the PHY supports. |
| 952 | * |
| 953 | * This updates the phylink's ethtool supported and advertising link mode |
| 954 | * masks. |
| 955 | * |
| 956 | * Returns 0 on success or a negative errno. |
| 957 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 958 | int phylink_connect_phy(struct phylink *pl, struct phy_device *phy) |
| 959 | { |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 960 | int ret; |
| 961 | |
Florian Fainelli | 4904b6e | 2017-12-12 16:00:26 -0800 | [diff] [blame] | 962 | /* Use PHY device/driver interface */ |
| 963 | if (pl->link_interface == PHY_INTERFACE_MODE_NA) { |
| 964 | pl->link_interface = phy->interface; |
| 965 | pl->link_config.interface = pl->link_interface; |
| 966 | } |
| 967 | |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 968 | ret = phylink_attach_phy(pl, phy, pl->link_interface); |
| 969 | if (ret < 0) |
| 970 | return ret; |
| 971 | |
Russell King | e45d1f5 | 2019-12-11 10:56:30 +0000 | [diff] [blame] | 972 | ret = phylink_bringup_phy(pl, phy, pl->link_config.interface); |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 973 | if (ret) |
| 974 | phy_detach(phy); |
| 975 | |
| 976 | return ret; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 977 | } |
| 978 | EXPORT_SYMBOL_GPL(phylink_connect_phy); |
| 979 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 980 | /** |
| 981 | * phylink_of_phy_connect() - connect the PHY specified in the DT mode. |
| 982 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 983 | * @dn: a pointer to a &struct device_node. |
Florian Fainelli | 0a62964 | 2017-12-12 16:00:25 -0800 | [diff] [blame] | 984 | * @flags: PHY-specific flags to communicate to the PHY device driver |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 985 | * |
| 986 | * Connect the phy specified in the device node @dn to the phylink instance |
| 987 | * specified by @pl. Actions specified in phylink_connect_phy() will be |
| 988 | * performed. |
| 989 | * |
| 990 | * Returns 0 on success or a negative errno. |
| 991 | */ |
Florian Fainelli | 0a62964 | 2017-12-12 16:00:25 -0800 | [diff] [blame] | 992 | int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn, |
| 993 | u32 flags) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 994 | { |
| 995 | struct device_node *phy_node; |
| 996 | struct phy_device *phy_dev; |
| 997 | int ret; |
| 998 | |
Russell King | cf4f267 | 2017-12-01 10:24:21 +0000 | [diff] [blame] | 999 | /* Fixed links and 802.3z are handled without needing a PHY */ |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1000 | if (pl->cfg_link_an_mode == MLO_AN_FIXED || |
| 1001 | (pl->cfg_link_an_mode == MLO_AN_INBAND && |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1002 | phy_interface_mode_is_8023z(pl->link_interface))) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1003 | return 0; |
| 1004 | |
| 1005 | phy_node = of_parse_phandle(dn, "phy-handle", 0); |
| 1006 | if (!phy_node) |
| 1007 | phy_node = of_parse_phandle(dn, "phy", 0); |
| 1008 | if (!phy_node) |
| 1009 | phy_node = of_parse_phandle(dn, "phy-device", 0); |
| 1010 | |
| 1011 | if (!phy_node) { |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1012 | if (pl->cfg_link_an_mode == MLO_AN_PHY) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1013 | return -ENODEV; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1014 | return 0; |
| 1015 | } |
| 1016 | |
Russell King | f5058a2 | 2019-12-12 17:16:12 +0000 | [diff] [blame] | 1017 | phy_dev = of_phy_find_device(phy_node); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1018 | /* We're done with the phy_node handle */ |
| 1019 | of_node_put(phy_node); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1020 | if (!phy_dev) |
| 1021 | return -ENODEV; |
| 1022 | |
Russell King | f5058a2 | 2019-12-12 17:16:12 +0000 | [diff] [blame] | 1023 | ret = phy_attach_direct(pl->netdev, phy_dev, flags, |
| 1024 | pl->link_interface); |
| 1025 | if (ret) |
| 1026 | return ret; |
| 1027 | |
Russell King | e45d1f5 | 2019-12-11 10:56:30 +0000 | [diff] [blame] | 1028 | ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1029 | if (ret) |
| 1030 | phy_detach(phy_dev); |
| 1031 | |
| 1032 | return ret; |
| 1033 | } |
| 1034 | EXPORT_SYMBOL_GPL(phylink_of_phy_connect); |
| 1035 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1036 | /** |
| 1037 | * phylink_disconnect_phy() - disconnect any PHY attached to the phylink |
| 1038 | * instance. |
| 1039 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1040 | * |
| 1041 | * Disconnect any current PHY from the phylink instance described by @pl. |
| 1042 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1043 | void phylink_disconnect_phy(struct phylink *pl) |
| 1044 | { |
| 1045 | struct phy_device *phy; |
| 1046 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1047 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1048 | |
| 1049 | phy = pl->phydev; |
| 1050 | if (phy) { |
| 1051 | mutex_lock(&phy->lock); |
| 1052 | mutex_lock(&pl->state_mutex); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1053 | pl->phydev = NULL; |
| 1054 | mutex_unlock(&pl->state_mutex); |
| 1055 | mutex_unlock(&phy->lock); |
| 1056 | flush_work(&pl->resolve); |
| 1057 | |
| 1058 | phy_disconnect(phy); |
| 1059 | } |
| 1060 | } |
| 1061 | EXPORT_SYMBOL_GPL(phylink_disconnect_phy); |
| 1062 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1063 | /** |
| 1064 | * phylink_mac_change() - notify phylink of a change in MAC state |
| 1065 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1066 | * @up: indicates whether the link is currently up. |
| 1067 | * |
| 1068 | * The MAC driver should call this driver when the state of its link |
| 1069 | * changes (eg, link failure, new negotiation results, etc.) |
| 1070 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1071 | void phylink_mac_change(struct phylink *pl, bool up) |
| 1072 | { |
| 1073 | if (!up) |
| 1074 | pl->mac_link_dropped = true; |
| 1075 | phylink_run_resolve(pl); |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1076 | phylink_dbg(pl, "mac link %s\n", up ? "up" : "down"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1077 | } |
| 1078 | EXPORT_SYMBOL_GPL(phylink_mac_change); |
| 1079 | |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 1080 | static irqreturn_t phylink_link_handler(int irq, void *data) |
| 1081 | { |
| 1082 | struct phylink *pl = data; |
| 1083 | |
| 1084 | phylink_run_resolve(pl); |
| 1085 | |
| 1086 | return IRQ_HANDLED; |
| 1087 | } |
| 1088 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1089 | /** |
| 1090 | * phylink_start() - start a phylink instance |
| 1091 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1092 | * |
| 1093 | * Start the phylink instance specified by @pl, configuring the MAC for the |
| 1094 | * desired link mode(s) and negotiation style. This should be called from the |
| 1095 | * network device driver's &struct net_device_ops ndo_open() method. |
| 1096 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1097 | void phylink_start(struct phylink *pl) |
| 1098 | { |
Russell King | 5c05c1d | 2020-04-23 17:02:56 +0100 | [diff] [blame] | 1099 | bool poll = false; |
| 1100 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1101 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1102 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1103 | phylink_info(pl, "configuring for %s/%s link mode\n", |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1104 | phylink_an_mode_str(pl->cur_link_an_mode), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1105 | phy_modes(pl->link_config.interface)); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1106 | |
Antoine Tenart | aeeb2e8 | 2018-09-19 11:39:31 +0200 | [diff] [blame] | 1107 | /* Always set the carrier off */ |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 1108 | if (pl->netdev) |
| 1109 | netif_carrier_off(pl->netdev); |
Antoine Tenart | aeeb2e8 | 2018-09-19 11:39:31 +0200 | [diff] [blame] | 1110 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1111 | /* Apply the link configuration to the MAC when starting. This allows |
| 1112 | * a fixed-link to start with the correct parameters, and also |
Colin Ian King | cc1122b | 2018-03-01 10:23:03 +0000 | [diff] [blame] | 1113 | * ensures that we set the appropriate advertisement for Serdes links. |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 1114 | * |
| 1115 | * Restart autonegotiation if using 802.3z to ensure that the link |
Russell King | 85b4394 | 2017-12-01 10:24:42 +0000 | [diff] [blame] | 1116 | * parameters are properly negotiated. This is necessary for DSA |
| 1117 | * switches using 802.3z negotiation to ensure they see our modes. |
| 1118 | */ |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 1119 | phylink_mac_initial_config(pl, true); |
Russell King | 85b4394 | 2017-12-01 10:24:42 +0000 | [diff] [blame] | 1120 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1121 | clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); |
| 1122 | phylink_run_resolve(pl); |
| 1123 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1124 | if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) { |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 1125 | int irq = gpiod_to_irq(pl->link_gpio); |
| 1126 | |
| 1127 | if (irq > 0) { |
| 1128 | if (!request_irq(irq, phylink_link_handler, |
| 1129 | IRQF_TRIGGER_RISING | |
| 1130 | IRQF_TRIGGER_FALLING, |
| 1131 | "netdev link", pl)) |
| 1132 | pl->link_irq = irq; |
| 1133 | else |
| 1134 | irq = 0; |
| 1135 | } |
| 1136 | if (irq <= 0) |
Russell King | 5c05c1d | 2020-04-23 17:02:56 +0100 | [diff] [blame] | 1137 | poll = true; |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 1138 | } |
Russell King | 5c05c1d | 2020-04-23 17:02:56 +0100 | [diff] [blame] | 1139 | |
| 1140 | switch (pl->cfg_link_an_mode) { |
| 1141 | case MLO_AN_FIXED: |
| 1142 | poll |= pl->config->poll_fixed_state; |
| 1143 | break; |
| 1144 | case MLO_AN_INBAND: |
| 1145 | poll |= pl->config->pcs_poll; |
| 1146 | break; |
| 1147 | } |
| 1148 | if (poll) |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 1149 | mod_timer(&pl->link_poll, jiffies + HZ); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1150 | if (pl->phydev) |
| 1151 | phy_start(pl->phydev); |
Arseny Solokha | c7fa7f5 | 2019-07-24 20:31:39 +0700 | [diff] [blame] | 1152 | if (pl->sfp_bus) |
| 1153 | sfp_upstream_start(pl->sfp_bus); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1154 | } |
| 1155 | EXPORT_SYMBOL_GPL(phylink_start); |
| 1156 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1157 | /** |
| 1158 | * phylink_stop() - stop a phylink instance |
| 1159 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1160 | * |
| 1161 | * Stop the phylink instance specified by @pl. This should be called from the |
| 1162 | * network device driver's &struct net_device_ops ndo_stop() method. The |
| 1163 | * network device's carrier state should not be changed prior to calling this |
| 1164 | * function. |
| 1165 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1166 | void phylink_stop(struct phylink *pl) |
| 1167 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1168 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1169 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1170 | if (pl->sfp_bus) |
| 1171 | sfp_upstream_stop(pl->sfp_bus); |
Arseny Solokha | c7fa7f5 | 2019-07-24 20:31:39 +0700 | [diff] [blame] | 1172 | if (pl->phydev) |
| 1173 | phy_stop(pl->phydev); |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 1174 | del_timer_sync(&pl->link_poll); |
| 1175 | if (pl->link_irq) { |
| 1176 | free_irq(pl->link_irq, pl); |
| 1177 | pl->link_irq = 0; |
| 1178 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1179 | |
Russell King | 87454b6 | 2019-02-11 15:04:24 +0000 | [diff] [blame] | 1180 | phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1181 | } |
| 1182 | EXPORT_SYMBOL_GPL(phylink_stop); |
| 1183 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1184 | /** |
| 1185 | * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY |
| 1186 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1187 | * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters |
| 1188 | * |
| 1189 | * Read the wake on lan parameters from the PHY attached to the phylink |
| 1190 | * instance specified by @pl. If no PHY is currently attached, report no |
| 1191 | * support for wake on lan. |
| 1192 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1193 | void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol) |
| 1194 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1195 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1196 | |
| 1197 | wol->supported = 0; |
| 1198 | wol->wolopts = 0; |
| 1199 | |
| 1200 | if (pl->phydev) |
| 1201 | phy_ethtool_get_wol(pl->phydev, wol); |
| 1202 | } |
| 1203 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol); |
| 1204 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1205 | /** |
| 1206 | * phylink_ethtool_set_wol() - set wake on lan parameters |
| 1207 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1208 | * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters |
| 1209 | * |
| 1210 | * Set the wake on lan parameters for the PHY attached to the phylink |
| 1211 | * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP |
| 1212 | * error. |
| 1213 | * |
| 1214 | * Returns zero on success or negative errno code. |
| 1215 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1216 | int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol) |
| 1217 | { |
| 1218 | int ret = -EOPNOTSUPP; |
| 1219 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1220 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1221 | |
| 1222 | if (pl->phydev) |
| 1223 | ret = phy_ethtool_set_wol(pl->phydev, wol); |
| 1224 | |
| 1225 | return ret; |
| 1226 | } |
| 1227 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol); |
| 1228 | |
| 1229 | static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b) |
| 1230 | { |
| 1231 | __ETHTOOL_DECLARE_LINK_MODE_MASK(mask); |
| 1232 | |
| 1233 | linkmode_zero(mask); |
| 1234 | phylink_set_port_modes(mask); |
| 1235 | |
| 1236 | linkmode_and(dst, dst, mask); |
| 1237 | linkmode_or(dst, dst, b); |
| 1238 | } |
| 1239 | |
| 1240 | static void phylink_get_ksettings(const struct phylink_link_state *state, |
| 1241 | struct ethtool_link_ksettings *kset) |
| 1242 | { |
| 1243 | phylink_merge_link_mode(kset->link_modes.advertising, state->advertising); |
| 1244 | linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising); |
| 1245 | kset->base.speed = state->speed; |
| 1246 | kset->base.duplex = state->duplex; |
| 1247 | kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE : |
| 1248 | AUTONEG_DISABLE; |
| 1249 | } |
| 1250 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1251 | /** |
| 1252 | * phylink_ethtool_ksettings_get() - get the current link settings |
| 1253 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1254 | * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings |
| 1255 | * |
| 1256 | * Read the current link settings for the phylink instance specified by @pl. |
| 1257 | * This will be the link settings read from the MAC, PHY or fixed link |
| 1258 | * settings depending on the current negotiation mode. |
| 1259 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1260 | int phylink_ethtool_ksettings_get(struct phylink *pl, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 1261 | struct ethtool_link_ksettings *kset) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1262 | { |
| 1263 | struct phylink_link_state link_state; |
| 1264 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1265 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1266 | |
| 1267 | if (pl->phydev) { |
| 1268 | phy_ethtool_ksettings_get(pl->phydev, kset); |
| 1269 | } else { |
| 1270 | kset->base.port = pl->link_port; |
| 1271 | } |
| 1272 | |
| 1273 | linkmode_copy(kset->link_modes.supported, pl->supported); |
| 1274 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1275 | switch (pl->cur_link_an_mode) { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1276 | case MLO_AN_FIXED: |
| 1277 | /* We are using fixed settings. Report these as the |
| 1278 | * current link settings - and note that these also |
| 1279 | * represent the supported speeds/duplex/pause modes. |
| 1280 | */ |
| 1281 | phylink_get_fixed_state(pl, &link_state); |
| 1282 | phylink_get_ksettings(&link_state, kset); |
| 1283 | break; |
| 1284 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1285 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1286 | /* If there is a phy attached, then use the reported |
| 1287 | * settings from the phy with no modification. |
| 1288 | */ |
| 1289 | if (pl->phydev) |
| 1290 | break; |
| 1291 | |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 1292 | phylink_mac_pcs_get_state(pl, &link_state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1293 | |
| 1294 | /* The MAC is reporting the link results from its own PCS |
| 1295 | * layer via in-band status. Report these as the current |
| 1296 | * link settings. |
| 1297 | */ |
| 1298 | phylink_get_ksettings(&link_state, kset); |
| 1299 | break; |
| 1300 | } |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |
| 1304 | EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get); |
| 1305 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1306 | /** |
| 1307 | * phylink_ethtool_ksettings_set() - set the link settings |
| 1308 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1309 | * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes |
| 1310 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1311 | int phylink_ethtool_ksettings_set(struct phylink *pl, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 1312 | const struct ethtool_link_ksettings *kset) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1313 | { |
Russell King | 7731676 | 2019-06-02 15:12:54 +0100 | [diff] [blame] | 1314 | __ETHTOOL_DECLARE_LINK_MODE_MASK(support); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1315 | struct phylink_link_state config; |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1316 | const struct phy_setting *s; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1317 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1318 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1319 | |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1320 | if (pl->phydev) { |
| 1321 | /* We can rely on phylib for this update; we also do not need |
| 1322 | * to update the pl->link_config settings: |
| 1323 | * - the configuration returned via ksettings_get() will come |
| 1324 | * from phylib whenever a PHY is present. |
| 1325 | * - link_config.interface will be updated by the PHY calling |
| 1326 | * back via phylink_phy_change() and a subsequent resolve. |
| 1327 | * - initial link configuration for PHY mode comes from the |
| 1328 | * last phy state updated via phylink_phy_change(). |
| 1329 | * - other configuration changes (e.g. pause modes) are |
| 1330 | * performed directly via phylib. |
| 1331 | * - if in in-band mode with a PHY, the link configuration |
| 1332 | * is passed on the link from the PHY, and all of |
| 1333 | * link_config.{speed,duplex,an_enabled,pause} are not used. |
| 1334 | * - the only possible use would be link_config.advertising |
| 1335 | * pause modes when in 1000base-X mode with a PHY, but in |
| 1336 | * the presence of a PHY, this should not be changed as that |
| 1337 | * should be determined from the media side advertisement. |
| 1338 | */ |
| 1339 | return phy_ethtool_ksettings_set(pl->phydev, kset); |
| 1340 | } |
| 1341 | |
Russell King | 7731676 | 2019-06-02 15:12:54 +0100 | [diff] [blame] | 1342 | linkmode_copy(support, pl->supported); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1343 | config = pl->link_config; |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1344 | config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1345 | |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1346 | /* Mask out unsupported advertisements, and force the autoneg bit */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1347 | linkmode_and(config.advertising, kset->link_modes.advertising, |
Russell King | 7731676 | 2019-06-02 15:12:54 +0100 | [diff] [blame] | 1348 | support); |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1349 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising, |
| 1350 | config.an_enabled); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1351 | |
| 1352 | /* FIXME: should we reject autoneg if phy/mac does not support it? */ |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1353 | switch (kset->base.autoneg) { |
| 1354 | case AUTONEG_DISABLE: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1355 | /* Autonegotiation disabled, select a suitable speed and |
| 1356 | * duplex. |
| 1357 | */ |
| 1358 | s = phy_lookup_setting(kset->base.speed, kset->base.duplex, |
Russell King | 7731676 | 2019-06-02 15:12:54 +0100 | [diff] [blame] | 1359 | support, false); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1360 | if (!s) |
| 1361 | return -EINVAL; |
| 1362 | |
| 1363 | /* If we have a fixed link (as specified by firmware), refuse |
| 1364 | * to change link parameters. |
| 1365 | */ |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1366 | if (pl->cur_link_an_mode == MLO_AN_FIXED && |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1367 | (s->speed != pl->link_config.speed || |
| 1368 | s->duplex != pl->link_config.duplex)) |
| 1369 | return -EINVAL; |
| 1370 | |
| 1371 | config.speed = s->speed; |
| 1372 | config.duplex = s->duplex; |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1373 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1374 | |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1375 | case AUTONEG_ENABLE: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1376 | /* If we have a fixed link, refuse to enable autonegotiation */ |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1377 | if (pl->cur_link_an_mode == MLO_AN_FIXED) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1378 | return -EINVAL; |
| 1379 | |
| 1380 | config.speed = SPEED_UNKNOWN; |
| 1381 | config.duplex = DUPLEX_UNKNOWN; |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1382 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1383 | |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1384 | default: |
| 1385 | return -EINVAL; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1388 | /* For a fixed link, this isn't able to change any parameters, |
| 1389 | * which just leaves inband mode. |
| 1390 | */ |
| 1391 | if (phylink_validate(pl, support, &config)) |
| 1392 | return -EINVAL; |
| 1393 | |
| 1394 | /* If autonegotiation is enabled, we must have an advertisement */ |
| 1395 | if (config.an_enabled && phylink_is_empty_linkmode(config.advertising)) |
| 1396 | return -EINVAL; |
| 1397 | |
| 1398 | mutex_lock(&pl->state_mutex); |
| 1399 | linkmode_copy(pl->link_config.advertising, config.advertising); |
| 1400 | pl->link_config.interface = config.interface; |
| 1401 | pl->link_config.speed = config.speed; |
| 1402 | pl->link_config.duplex = config.duplex; |
Russell King | a83c882 | 2020-07-21 12:04:26 +0100 | [diff] [blame^] | 1403 | pl->link_config.an_enabled = config.an_enabled; |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1404 | |
| 1405 | if (pl->cur_link_an_mode == MLO_AN_INBAND && |
| 1406 | !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) { |
| 1407 | /* If in 802.3z mode, this updates the advertisement. |
| 1408 | * |
| 1409 | * If we are in SGMII mode without a PHY, there is no |
| 1410 | * advertisement; the only thing we have is the pause |
| 1411 | * modes which can only come from a PHY. |
Russell King | 5d57c32 | 2019-12-13 18:22:02 +0000 | [diff] [blame] | 1412 | */ |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1413 | phylink_pcs_config(pl, true, &pl->link_config); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1414 | } |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1415 | mutex_unlock(&pl->state_mutex); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1416 | |
Dan Carpenter | d18c2a1 | 2017-08-10 00:35:50 +0300 | [diff] [blame] | 1417 | return 0; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1418 | } |
| 1419 | EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set); |
| 1420 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1421 | /** |
| 1422 | * phylink_ethtool_nway_reset() - restart negotiation |
| 1423 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1424 | * |
| 1425 | * Restart negotiation for the phylink instance specified by @pl. This will |
| 1426 | * cause any attached phy to restart negotiation with the link partner, and |
| 1427 | * if the MAC is in a BaseX mode, the MAC will also be requested to restart |
| 1428 | * negotiation. |
| 1429 | * |
| 1430 | * Returns zero on success, or negative error code. |
| 1431 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1432 | int phylink_ethtool_nway_reset(struct phylink *pl) |
| 1433 | { |
| 1434 | int ret = 0; |
| 1435 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1436 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1437 | |
| 1438 | if (pl->phydev) |
| 1439 | ret = phy_restart_aneg(pl->phydev); |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 1440 | phylink_mac_pcs_an_restart(pl); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1441 | |
| 1442 | return ret; |
| 1443 | } |
| 1444 | EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset); |
| 1445 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1446 | /** |
| 1447 | * phylink_ethtool_get_pauseparam() - get the current pause parameters |
| 1448 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1449 | * @pause: a pointer to a &struct ethtool_pauseparam |
| 1450 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1451 | void phylink_ethtool_get_pauseparam(struct phylink *pl, |
| 1452 | struct ethtool_pauseparam *pause) |
| 1453 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1454 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1455 | |
| 1456 | pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN); |
| 1457 | pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX); |
| 1458 | pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX); |
| 1459 | } |
| 1460 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam); |
| 1461 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1462 | /** |
| 1463 | * phylink_ethtool_set_pauseparam() - set the current pause parameters |
| 1464 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1465 | * @pause: a pointer to a &struct ethtool_pauseparam |
| 1466 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1467 | int phylink_ethtool_set_pauseparam(struct phylink *pl, |
| 1468 | struct ethtool_pauseparam *pause) |
| 1469 | { |
| 1470 | struct phylink_link_state *config = &pl->link_config; |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1471 | bool manual_changed; |
| 1472 | int pause_state; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1473 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1474 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1475 | |
Russell King | 8cdfa25 | 2020-02-15 15:49:37 +0000 | [diff] [blame] | 1476 | if (pl->cur_link_an_mode == MLO_AN_FIXED) |
| 1477 | return -EOPNOTSUPP; |
| 1478 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1479 | if (!phylink_test(pl->supported, Pause) && |
| 1480 | !phylink_test(pl->supported, Asym_Pause)) |
| 1481 | return -EOPNOTSUPP; |
| 1482 | |
| 1483 | if (!phylink_test(pl->supported, Asym_Pause) && |
| 1484 | !pause->autoneg && pause->rx_pause != pause->tx_pause) |
| 1485 | return -EINVAL; |
| 1486 | |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1487 | pause_state = 0; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1488 | if (pause->autoneg) |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1489 | pause_state |= MLO_PAUSE_AN; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1490 | if (pause->rx_pause) |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1491 | pause_state |= MLO_PAUSE_RX; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1492 | if (pause->tx_pause) |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1493 | pause_state |= MLO_PAUSE_TX; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1494 | |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1495 | mutex_lock(&pl->state_mutex); |
Russell King | f904f15 | 2020-02-15 15:49:58 +0000 | [diff] [blame] | 1496 | /* |
| 1497 | * See the comments for linkmode_set_pause(), wrt the deficiencies |
| 1498 | * with the current implementation. A solution to this issue would |
| 1499 | * be: |
| 1500 | * ethtool Local device |
| 1501 | * rx tx Pause AsymDir |
| 1502 | * 0 0 0 0 |
| 1503 | * 1 0 1 1 |
| 1504 | * 0 1 0 1 |
| 1505 | * 1 1 1 1 |
| 1506 | * and then use the ethtool rx/tx enablement status to mask the |
| 1507 | * rx/tx pause resolution. |
| 1508 | */ |
| 1509 | linkmode_set_pause(config->advertising, pause->tx_pause, |
| 1510 | pause->rx_pause); |
| 1511 | |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1512 | manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN || |
| 1513 | (!(pause_state & MLO_PAUSE_AN) && |
| 1514 | (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK); |
| 1515 | |
| 1516 | config->pause = pause_state; |
| 1517 | |
Russell King | c718af2 | 2020-06-23 17:47:23 +0100 | [diff] [blame] | 1518 | if (!pl->phydev && !test_bit(PHYLINK_DISABLE_STOPPED, |
| 1519 | &pl->phylink_disable_state)) |
| 1520 | phylink_pcs_config(pl, true, &pl->link_config); |
| 1521 | |
| 1522 | mutex_unlock(&pl->state_mutex); |
| 1523 | |
| 1524 | /* If we have a PHY, a change of the pause frame advertisement will |
| 1525 | * cause phylib to renegotiate (if AN is enabled) which will in turn |
| 1526 | * call our phylink_phy_change() and trigger a resolve. Note that |
| 1527 | * we can't hold our state mutex while calling phy_set_asym_pause(). |
Russell King | d9922c0 | 2019-11-19 17:28:14 +0000 | [diff] [blame] | 1528 | */ |
Russell King | c718af2 | 2020-06-23 17:47:23 +0100 | [diff] [blame] | 1529 | if (pl->phydev) |
Russell King | d9922c0 | 2019-11-19 17:28:14 +0000 | [diff] [blame] | 1530 | phy_set_asym_pause(pl->phydev, pause->rx_pause, |
| 1531 | pause->tx_pause); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1532 | |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1533 | /* If the manual pause settings changed, make sure we trigger a |
| 1534 | * resolve to update their state; we can not guarantee that the |
| 1535 | * link will cycle. |
| 1536 | */ |
| 1537 | if (manual_changed) { |
| 1538 | pl->mac_link_dropped = true; |
| 1539 | phylink_run_resolve(pl); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1540 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam); |
| 1545 | |
| 1546 | /** |
| 1547 | * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error |
| 1548 | * counter |
| 1549 | * @pl: a pointer to a &struct phylink returned from phylink_create(). |
| 1550 | * |
| 1551 | * Read the Energy Efficient Ethernet error counter from the PHY associated |
| 1552 | * with the phylink instance specified by @pl. |
| 1553 | * |
| 1554 | * Returns positive error counter value, or negative error code. |
| 1555 | */ |
| 1556 | int phylink_get_eee_err(struct phylink *pl) |
| 1557 | { |
| 1558 | int ret = 0; |
| 1559 | |
| 1560 | ASSERT_RTNL(); |
| 1561 | |
| 1562 | if (pl->phydev) |
| 1563 | ret = phy_get_eee_err(pl->phydev); |
| 1564 | |
| 1565 | return ret; |
| 1566 | } |
| 1567 | EXPORT_SYMBOL_GPL(phylink_get_eee_err); |
| 1568 | |
| 1569 | /** |
Russell King | 86e5813 | 2019-02-11 11:46:06 +0000 | [diff] [blame] | 1570 | * phylink_init_eee() - init and check the EEE features |
| 1571 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1572 | * @clk_stop_enable: allow PHY to stop receive clock |
| 1573 | * |
| 1574 | * Must be called either with RTNL held or within mac_link_up() |
| 1575 | */ |
| 1576 | int phylink_init_eee(struct phylink *pl, bool clk_stop_enable) |
| 1577 | { |
| 1578 | int ret = -EOPNOTSUPP; |
| 1579 | |
| 1580 | if (pl->phydev) |
| 1581 | ret = phy_init_eee(pl->phydev, clk_stop_enable); |
| 1582 | |
| 1583 | return ret; |
| 1584 | } |
| 1585 | EXPORT_SYMBOL_GPL(phylink_init_eee); |
| 1586 | |
| 1587 | /** |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1588 | * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters |
| 1589 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1590 | * @eee: a pointer to a &struct ethtool_eee for the read parameters |
| 1591 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1592 | int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee) |
| 1593 | { |
| 1594 | int ret = -EOPNOTSUPP; |
| 1595 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1596 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1597 | |
| 1598 | if (pl->phydev) |
| 1599 | ret = phy_ethtool_get_eee(pl->phydev, eee); |
| 1600 | |
| 1601 | return ret; |
| 1602 | } |
| 1603 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee); |
| 1604 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1605 | /** |
| 1606 | * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters |
| 1607 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1608 | * @eee: a pointer to a &struct ethtool_eee for the desired parameters |
| 1609 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1610 | int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee) |
| 1611 | { |
| 1612 | int ret = -EOPNOTSUPP; |
| 1613 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1614 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1615 | |
| 1616 | if (pl->phydev) |
| 1617 | ret = phy_ethtool_set_eee(pl->phydev, eee); |
| 1618 | |
| 1619 | return ret; |
| 1620 | } |
| 1621 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee); |
| 1622 | |
| 1623 | /* This emulates MII registers for a fixed-mode phy operating as per the |
| 1624 | * passed in state. "aneg" defines if we report negotiation is possible. |
| 1625 | * |
| 1626 | * FIXME: should deal with negotiation state too. |
| 1627 | */ |
Russell King | 7fdc455 | 2019-05-28 10:57:18 +0100 | [diff] [blame] | 1628 | static int phylink_mii_emul_read(unsigned int reg, |
| 1629 | struct phylink_link_state *state) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1630 | { |
| 1631 | struct fixed_phy_status fs; |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 1632 | unsigned long *lpa = state->lp_advertising; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1633 | int val; |
| 1634 | |
| 1635 | fs.link = state->link; |
| 1636 | fs.speed = state->speed; |
| 1637 | fs.duplex = state->duplex; |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 1638 | fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa); |
| 1639 | fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1640 | |
| 1641 | val = swphy_read_reg(reg, &fs); |
| 1642 | if (reg == MII_BMSR) { |
| 1643 | if (!state->an_complete) |
| 1644 | val &= ~BMSR_ANEGCOMPLETE; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1645 | } |
| 1646 | return val; |
| 1647 | } |
| 1648 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1649 | static int phylink_phy_read(struct phylink *pl, unsigned int phy_id, |
| 1650 | unsigned int reg) |
| 1651 | { |
| 1652 | struct phy_device *phydev = pl->phydev; |
| 1653 | int prtad, devad; |
| 1654 | |
| 1655 | if (mdio_phy_id_is_c45(phy_id)) { |
| 1656 | prtad = mdio_phy_id_prtad(phy_id); |
| 1657 | devad = mdio_phy_id_devad(phy_id); |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 1658 | devad = mdiobus_c45_addr(devad, reg); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1659 | } else if (phydev->is_c45) { |
| 1660 | switch (reg) { |
| 1661 | case MII_BMCR: |
| 1662 | case MII_BMSR: |
| 1663 | case MII_PHYSID1: |
| 1664 | case MII_PHYSID2: |
Russell King | 320ed3b | 2020-06-18 14:46:08 +0100 | [diff] [blame] | 1665 | devad = __ffs(phydev->c45_ids.mmds_present); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1666 | break; |
| 1667 | case MII_ADVERTISE: |
| 1668 | case MII_LPA: |
Russell King | 320ed3b | 2020-06-18 14:46:08 +0100 | [diff] [blame] | 1669 | if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN)) |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1670 | return -EINVAL; |
| 1671 | devad = MDIO_MMD_AN; |
| 1672 | if (reg == MII_ADVERTISE) |
| 1673 | reg = MDIO_AN_ADVERTISE; |
| 1674 | else |
| 1675 | reg = MDIO_AN_LPA; |
| 1676 | break; |
| 1677 | default: |
| 1678 | return -EINVAL; |
| 1679 | } |
| 1680 | prtad = phy_id; |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 1681 | devad = mdiobus_c45_addr(devad, reg); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1682 | } else { |
| 1683 | prtad = phy_id; |
| 1684 | devad = reg; |
| 1685 | } |
| 1686 | return mdiobus_read(pl->phydev->mdio.bus, prtad, devad); |
| 1687 | } |
| 1688 | |
| 1689 | static int phylink_phy_write(struct phylink *pl, unsigned int phy_id, |
| 1690 | unsigned int reg, unsigned int val) |
| 1691 | { |
| 1692 | struct phy_device *phydev = pl->phydev; |
| 1693 | int prtad, devad; |
| 1694 | |
| 1695 | if (mdio_phy_id_is_c45(phy_id)) { |
| 1696 | prtad = mdio_phy_id_prtad(phy_id); |
| 1697 | devad = mdio_phy_id_devad(phy_id); |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 1698 | devad = mdiobus_c45_addr(devad, reg); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1699 | } else if (phydev->is_c45) { |
| 1700 | switch (reg) { |
| 1701 | case MII_BMCR: |
| 1702 | case MII_BMSR: |
| 1703 | case MII_PHYSID1: |
| 1704 | case MII_PHYSID2: |
Russell King | 320ed3b | 2020-06-18 14:46:08 +0100 | [diff] [blame] | 1705 | devad = __ffs(phydev->c45_ids.mmds_present); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1706 | break; |
| 1707 | case MII_ADVERTISE: |
| 1708 | case MII_LPA: |
Russell King | 320ed3b | 2020-06-18 14:46:08 +0100 | [diff] [blame] | 1709 | if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN)) |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1710 | return -EINVAL; |
| 1711 | devad = MDIO_MMD_AN; |
| 1712 | if (reg == MII_ADVERTISE) |
| 1713 | reg = MDIO_AN_ADVERTISE; |
| 1714 | else |
| 1715 | reg = MDIO_AN_LPA; |
| 1716 | break; |
| 1717 | default: |
| 1718 | return -EINVAL; |
| 1719 | } |
| 1720 | prtad = phy_id; |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 1721 | devad = mdiobus_c45_addr(devad, reg); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1722 | } else { |
| 1723 | prtad = phy_id; |
| 1724 | devad = reg; |
| 1725 | } |
| 1726 | |
| 1727 | return mdiobus_write(phydev->mdio.bus, prtad, devad, val); |
| 1728 | } |
| 1729 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1730 | static int phylink_mii_read(struct phylink *pl, unsigned int phy_id, |
| 1731 | unsigned int reg) |
| 1732 | { |
| 1733 | struct phylink_link_state state; |
| 1734 | int val = 0xffff; |
| 1735 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1736 | switch (pl->cur_link_an_mode) { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1737 | case MLO_AN_FIXED: |
| 1738 | if (phy_id == 0) { |
| 1739 | phylink_get_fixed_state(pl, &state); |
Russell King | 7fdc455 | 2019-05-28 10:57:18 +0100 | [diff] [blame] | 1740 | val = phylink_mii_emul_read(reg, &state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1741 | } |
| 1742 | break; |
| 1743 | |
| 1744 | case MLO_AN_PHY: |
| 1745 | return -EOPNOTSUPP; |
| 1746 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1747 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1748 | if (phy_id == 0) { |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 1749 | phylink_mac_pcs_get_state(pl, &state); |
Russell King | 7fdc455 | 2019-05-28 10:57:18 +0100 | [diff] [blame] | 1750 | val = phylink_mii_emul_read(reg, &state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1751 | } |
| 1752 | break; |
| 1753 | } |
| 1754 | |
| 1755 | return val & 0xffff; |
| 1756 | } |
| 1757 | |
| 1758 | static int phylink_mii_write(struct phylink *pl, unsigned int phy_id, |
| 1759 | unsigned int reg, unsigned int val) |
| 1760 | { |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1761 | switch (pl->cur_link_an_mode) { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1762 | case MLO_AN_FIXED: |
| 1763 | break; |
| 1764 | |
| 1765 | case MLO_AN_PHY: |
| 1766 | return -EOPNOTSUPP; |
| 1767 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1768 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1769 | break; |
| 1770 | } |
| 1771 | |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1775 | /** |
| 1776 | * phylink_mii_ioctl() - generic mii ioctl interface |
| 1777 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1778 | * @ifr: a pointer to a &struct ifreq for socket ioctls |
| 1779 | * @cmd: ioctl cmd to execute |
| 1780 | * |
| 1781 | * Perform the specified MII ioctl on the PHY attached to the phylink instance |
| 1782 | * specified by @pl. If no PHY is attached, emulate the presence of the PHY. |
| 1783 | * |
| 1784 | * Returns: zero on success or negative error code. |
| 1785 | * |
| 1786 | * %SIOCGMIIPHY: |
| 1787 | * read register from the current PHY. |
| 1788 | * %SIOCGMIIREG: |
| 1789 | * read register from the specified PHY. |
| 1790 | * %SIOCSMIIREG: |
| 1791 | * set a register on the specified PHY. |
| 1792 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1793 | int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd) |
| 1794 | { |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1795 | struct mii_ioctl_data *mii = if_mii(ifr); |
| 1796 | int ret; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1797 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1798 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1799 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1800 | if (pl->phydev) { |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1801 | /* PHYs only exist for MLO_AN_PHY and SGMII */ |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1802 | switch (cmd) { |
| 1803 | case SIOCGMIIPHY: |
| 1804 | mii->phy_id = pl->phydev->mdio.addr; |
Gustavo A. R. Silva | 46cd750 | 2018-01-05 11:23:45 -0600 | [diff] [blame] | 1805 | /* fall through */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1806 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1807 | case SIOCGMIIREG: |
| 1808 | ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num); |
| 1809 | if (ret >= 0) { |
| 1810 | mii->val_out = ret; |
| 1811 | ret = 0; |
| 1812 | } |
| 1813 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1814 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1815 | case SIOCSMIIREG: |
| 1816 | ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num, |
| 1817 | mii->val_in); |
| 1818 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1819 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1820 | default: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1821 | ret = phy_mii_ioctl(pl->phydev, ifr, cmd); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1822 | break; |
| 1823 | } |
| 1824 | } else { |
| 1825 | switch (cmd) { |
| 1826 | case SIOCGMIIPHY: |
| 1827 | mii->phy_id = 0; |
Gustavo A. R. Silva | 46cd750 | 2018-01-05 11:23:45 -0600 | [diff] [blame] | 1828 | /* fall through */ |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1829 | |
| 1830 | case SIOCGMIIREG: |
| 1831 | ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num); |
| 1832 | if (ret >= 0) { |
| 1833 | mii->val_out = ret; |
| 1834 | ret = 0; |
| 1835 | } |
| 1836 | break; |
| 1837 | |
| 1838 | case SIOCSMIIREG: |
| 1839 | ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num, |
| 1840 | mii->val_in); |
| 1841 | break; |
| 1842 | |
| 1843 | default: |
| 1844 | ret = -EOPNOTSUPP; |
| 1845 | break; |
| 1846 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | return ret; |
| 1850 | } |
| 1851 | EXPORT_SYMBOL_GPL(phylink_mii_ioctl); |
| 1852 | |
Russell King | c6d5d84 | 2020-06-24 11:06:54 +0100 | [diff] [blame] | 1853 | /** |
| 1854 | * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both |
| 1855 | * link partners |
| 1856 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1857 | * @sync: perform action synchronously |
| 1858 | * |
| 1859 | * If we have a PHY that is not part of a SFP module, then set the speed |
| 1860 | * as described in the phy_speed_down() function. Please see this function |
| 1861 | * for a description of the @sync parameter. |
| 1862 | * |
| 1863 | * Returns zero if there is no PHY, otherwise as per phy_speed_down(). |
| 1864 | */ |
| 1865 | int phylink_speed_down(struct phylink *pl, bool sync) |
| 1866 | { |
| 1867 | int ret = 0; |
| 1868 | |
| 1869 | ASSERT_RTNL(); |
| 1870 | |
| 1871 | if (!pl->sfp_bus && pl->phydev) |
| 1872 | ret = phy_speed_down(pl->phydev, sync); |
| 1873 | |
| 1874 | return ret; |
| 1875 | } |
| 1876 | EXPORT_SYMBOL_GPL(phylink_speed_down); |
| 1877 | |
| 1878 | /** |
| 1879 | * phylink_speed_up() - restore the advertised speeds prior to the call to |
| 1880 | * phylink_speed_down() |
| 1881 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1882 | * |
| 1883 | * If we have a PHY that is not part of a SFP module, then restore the |
| 1884 | * PHY speeds as per phy_speed_up(). |
| 1885 | * |
| 1886 | * Returns zero if there is no PHY, otherwise as per phy_speed_up(). |
| 1887 | */ |
| 1888 | int phylink_speed_up(struct phylink *pl) |
| 1889 | { |
| 1890 | int ret = 0; |
| 1891 | |
| 1892 | ASSERT_RTNL(); |
| 1893 | |
| 1894 | if (!pl->sfp_bus && pl->phydev) |
| 1895 | ret = phy_speed_up(pl->phydev); |
| 1896 | |
| 1897 | return ret; |
| 1898 | } |
| 1899 | EXPORT_SYMBOL_GPL(phylink_speed_up); |
| 1900 | |
Russell King | 320587e6 | 2019-05-28 10:57:34 +0100 | [diff] [blame] | 1901 | static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus) |
| 1902 | { |
| 1903 | struct phylink *pl = upstream; |
| 1904 | |
| 1905 | pl->netdev->sfp_bus = bus; |
| 1906 | } |
| 1907 | |
| 1908 | static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus) |
| 1909 | { |
| 1910 | struct phylink *pl = upstream; |
| 1911 | |
| 1912 | pl->netdev->sfp_bus = NULL; |
| 1913 | } |
| 1914 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 1915 | static int phylink_sfp_config(struct phylink *pl, u8 mode, |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1916 | const unsigned long *supported, |
| 1917 | const unsigned long *advertising) |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1918 | { |
Russell King | 7731676 | 2019-06-02 15:12:54 +0100 | [diff] [blame] | 1919 | __ETHTOOL_DECLARE_LINK_MODE_MASK(support1); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1920 | __ETHTOOL_DECLARE_LINK_MODE_MASK(support); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1921 | struct phylink_link_state config; |
| 1922 | phy_interface_t iface; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1923 | bool changed; |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1924 | int ret; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1925 | |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1926 | linkmode_copy(support, supported); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1927 | |
| 1928 | memset(&config, 0, sizeof(config)); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1929 | linkmode_copy(config.advertising, advertising); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 1930 | config.interface = PHY_INTERFACE_MODE_NA; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1931 | config.speed = SPEED_UNKNOWN; |
| 1932 | config.duplex = DUPLEX_UNKNOWN; |
| 1933 | config.pause = MLO_PAUSE_AN; |
| 1934 | config.an_enabled = pl->link_config.an_enabled; |
| 1935 | |
| 1936 | /* Ignore errors if we're expecting a PHY to attach later */ |
| 1937 | ret = phylink_validate(pl, support, &config); |
| 1938 | if (ret) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1939 | phylink_err(pl, "validation with support %*pb failed: %d\n", |
| 1940 | __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 1941 | return ret; |
| 1942 | } |
| 1943 | |
Russell King | a4516c7 | 2019-12-11 10:55:59 +0000 | [diff] [blame] | 1944 | iface = sfp_select_interface(pl->sfp_bus, config.advertising); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 1945 | if (iface == PHY_INTERFACE_MODE_NA) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1946 | phylink_err(pl, |
| 1947 | "selection of interface failed, advertisement %*pb\n", |
| 1948 | __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 1949 | return -EINVAL; |
| 1950 | } |
| 1951 | |
| 1952 | config.interface = iface; |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1953 | linkmode_copy(support1, support); |
Russell King | 7731676 | 2019-06-02 15:12:54 +0100 | [diff] [blame] | 1954 | ret = phylink_validate(pl, support1, &config); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 1955 | if (ret) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1956 | phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n", |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1957 | phylink_an_mode_str(mode), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1958 | phy_modes(config.interface), |
| 1959 | __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1960 | return ret; |
| 1961 | } |
| 1962 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1963 | phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n", |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1964 | phylink_an_mode_str(mode), phy_modes(config.interface), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1965 | __ETHTOOL_LINK_MODE_MASK_NBITS, support); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1966 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1967 | if (phy_interface_mode_is_8023z(iface) && pl->phydev) |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1968 | return -EINVAL; |
| 1969 | |
Russell King | 554032c | 2019-10-15 11:28:46 +0100 | [diff] [blame] | 1970 | changed = !linkmode_equal(pl->supported, support); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1971 | if (changed) { |
| 1972 | linkmode_copy(pl->supported, support); |
| 1973 | linkmode_copy(pl->link_config.advertising, config.advertising); |
| 1974 | } |
| 1975 | |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1976 | if (pl->cur_link_an_mode != mode || |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1977 | pl->link_config.interface != config.interface) { |
| 1978 | pl->link_config.interface = config.interface; |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1979 | pl->cur_link_an_mode = mode; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1980 | |
| 1981 | changed = true; |
| 1982 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1983 | phylink_info(pl, "switched to %s/%s link mode\n", |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1984 | phylink_an_mode_str(mode), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1985 | phy_modes(config.interface)); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1986 | } |
| 1987 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 1988 | pl->link_port = pl->sfp_port; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1989 | |
| 1990 | if (changed && !test_bit(PHYLINK_DISABLE_STOPPED, |
| 1991 | &pl->phylink_disable_state)) |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 1992 | phylink_mac_initial_config(pl, false); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1993 | |
| 1994 | return ret; |
| 1995 | } |
| 1996 | |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 1997 | static int phylink_sfp_module_insert(void *upstream, |
| 1998 | const struct sfp_eeprom_id *id) |
| 1999 | { |
| 2000 | struct phylink *pl = upstream; |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2001 | unsigned long *support = pl->sfp_support; |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2002 | |
| 2003 | ASSERT_RTNL(); |
| 2004 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2005 | linkmode_zero(support); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2006 | sfp_parse_support(pl->sfp_bus, id, support); |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2007 | pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2008 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2009 | /* If this module may have a PHY connecting later, defer until later */ |
| 2010 | pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id); |
| 2011 | if (pl->sfp_may_have_phy) |
| 2012 | return 0; |
| 2013 | |
| 2014 | return phylink_sfp_config(pl, MLO_AN_INBAND, support, support); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2017 | static int phylink_sfp_module_start(void *upstream) |
| 2018 | { |
| 2019 | struct phylink *pl = upstream; |
| 2020 | |
| 2021 | /* If this SFP module has a PHY, start the PHY now. */ |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2022 | if (pl->phydev) { |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2023 | phy_start(pl->phydev); |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2024 | return 0; |
| 2025 | } |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2026 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2027 | /* If the module may have a PHY but we didn't detect one we |
| 2028 | * need to configure the MAC here. |
| 2029 | */ |
| 2030 | if (!pl->sfp_may_have_phy) |
| 2031 | return 0; |
| 2032 | |
| 2033 | return phylink_sfp_config(pl, MLO_AN_INBAND, |
| 2034 | pl->sfp_support, pl->sfp_support); |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | static void phylink_sfp_module_stop(void *upstream) |
| 2038 | { |
| 2039 | struct phylink *pl = upstream; |
| 2040 | |
| 2041 | /* If this SFP module has a PHY, stop it. */ |
| 2042 | if (pl->phydev) |
| 2043 | phy_stop(pl->phydev); |
| 2044 | } |
| 2045 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2046 | static void phylink_sfp_link_down(void *upstream) |
| 2047 | { |
| 2048 | struct phylink *pl = upstream; |
| 2049 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 2050 | ASSERT_RTNL(); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2051 | |
Russell King | 87454b6 | 2019-02-11 15:04:24 +0000 | [diff] [blame] | 2052 | phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | static void phylink_sfp_link_up(void *upstream) |
| 2056 | { |
| 2057 | struct phylink *pl = upstream; |
| 2058 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 2059 | ASSERT_RTNL(); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2060 | |
| 2061 | clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state); |
| 2062 | phylink_run_resolve(pl); |
| 2063 | } |
| 2064 | |
Russell King | 7adb5b2 | 2019-12-11 10:56:50 +0000 | [diff] [blame] | 2065 | /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII |
| 2066 | * or 802.3z control word, so inband will not work. |
| 2067 | */ |
| 2068 | static bool phylink_phy_no_inband(struct phy_device *phy) |
| 2069 | { |
| 2070 | return phy->is_c45 && |
| 2071 | (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150; |
| 2072 | } |
| 2073 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2074 | static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy) |
| 2075 | { |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 2076 | struct phylink *pl = upstream; |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2077 | phy_interface_t interface; |
Russell King | 7adb5b2 | 2019-12-11 10:56:50 +0000 | [diff] [blame] | 2078 | u8 mode; |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 2079 | int ret; |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 2080 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2081 | /* |
| 2082 | * This is the new way of dealing with flow control for PHYs, |
| 2083 | * as described by Timur Tabi in commit 529ed1275263 ("net: phy: |
| 2084 | * phy drivers should not set SUPPORTED_[Asym_]Pause") except |
| 2085 | * using our validate call to the MAC, we rely upon the MAC |
| 2086 | * clearing the bits from both supported and advertising fields. |
| 2087 | */ |
| 2088 | phy_support_asym_pause(phy); |
| 2089 | |
Russell King | 7adb5b2 | 2019-12-11 10:56:50 +0000 | [diff] [blame] | 2090 | if (phylink_phy_no_inband(phy)) |
| 2091 | mode = MLO_AN_PHY; |
| 2092 | else |
| 2093 | mode = MLO_AN_INBAND; |
| 2094 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2095 | /* Do the initial configuration */ |
Russell King | 7adb5b2 | 2019-12-11 10:56:50 +0000 | [diff] [blame] | 2096 | ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising); |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2097 | if (ret < 0) |
| 2098 | return ret; |
| 2099 | |
| 2100 | interface = pl->link_config.interface; |
| 2101 | ret = phylink_attach_phy(pl, phy, interface); |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 2102 | if (ret < 0) |
| 2103 | return ret; |
| 2104 | |
Russell King | e45d1f5 | 2019-12-11 10:56:30 +0000 | [diff] [blame] | 2105 | ret = phylink_bringup_phy(pl, phy, interface); |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 2106 | if (ret) |
| 2107 | phy_detach(phy); |
| 2108 | |
| 2109 | return ret; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | static void phylink_sfp_disconnect_phy(void *upstream) |
| 2113 | { |
| 2114 | phylink_disconnect_phy(upstream); |
| 2115 | } |
| 2116 | |
| 2117 | static const struct sfp_upstream_ops sfp_phylink_ops = { |
Russell King | 320587e6 | 2019-05-28 10:57:34 +0100 | [diff] [blame] | 2118 | .attach = phylink_sfp_attach, |
| 2119 | .detach = phylink_sfp_detach, |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2120 | .module_insert = phylink_sfp_module_insert, |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2121 | .module_start = phylink_sfp_module_start, |
| 2122 | .module_stop = phylink_sfp_module_stop, |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2123 | .link_up = phylink_sfp_link_up, |
| 2124 | .link_down = phylink_sfp_link_down, |
| 2125 | .connect_phy = phylink_sfp_connect_phy, |
| 2126 | .disconnect_phy = phylink_sfp_disconnect_phy, |
| 2127 | }; |
| 2128 | |
Russell King | 624c0f0 | 2018-08-09 15:38:38 +0200 | [diff] [blame] | 2129 | /* Helpers for MAC drivers */ |
| 2130 | |
| 2131 | /** |
| 2132 | * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper |
| 2133 | * @state: a pointer to a &struct phylink_link_state |
| 2134 | * |
| 2135 | * Inspect the interface mode, advertising mask or forced speed and |
| 2136 | * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching |
| 2137 | * the interface mode to suit. @state->interface is appropriately |
| 2138 | * updated, and the advertising mask has the "other" baseX_Full flag |
| 2139 | * cleared. |
| 2140 | */ |
| 2141 | void phylink_helper_basex_speed(struct phylink_link_state *state) |
| 2142 | { |
| 2143 | if (phy_interface_mode_is_8023z(state->interface)) { |
| 2144 | bool want_2500 = state->an_enabled ? |
| 2145 | phylink_test(state->advertising, 2500baseX_Full) : |
| 2146 | state->speed == SPEED_2500; |
| 2147 | |
| 2148 | if (want_2500) { |
| 2149 | phylink_clear(state->advertising, 1000baseX_Full); |
| 2150 | state->interface = PHY_INTERFACE_MODE_2500BASEX; |
| 2151 | } else { |
| 2152 | phylink_clear(state->advertising, 2500baseX_Full); |
| 2153 | state->interface = PHY_INTERFACE_MODE_1000BASEX; |
| 2154 | } |
| 2155 | } |
| 2156 | } |
| 2157 | EXPORT_SYMBOL_GPL(phylink_helper_basex_speed); |
| 2158 | |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2159 | static void phylink_decode_c37_word(struct phylink_link_state *state, |
| 2160 | uint16_t config_reg, int speed) |
| 2161 | { |
| 2162 | bool tx_pause, rx_pause; |
| 2163 | int fd_bit; |
| 2164 | |
| 2165 | if (speed == SPEED_2500) |
| 2166 | fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT; |
| 2167 | else |
| 2168 | fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT; |
| 2169 | |
| 2170 | mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit); |
| 2171 | |
| 2172 | if (linkmode_test_bit(fd_bit, state->advertising) && |
| 2173 | linkmode_test_bit(fd_bit, state->lp_advertising)) { |
| 2174 | state->speed = speed; |
| 2175 | state->duplex = DUPLEX_FULL; |
| 2176 | } else { |
| 2177 | /* negotiation failure */ |
| 2178 | state->link = false; |
| 2179 | } |
| 2180 | |
| 2181 | linkmode_resolve_pause(state->advertising, state->lp_advertising, |
| 2182 | &tx_pause, &rx_pause); |
| 2183 | |
| 2184 | if (tx_pause) |
| 2185 | state->pause |= MLO_PAUSE_TX; |
| 2186 | if (rx_pause) |
| 2187 | state->pause |= MLO_PAUSE_RX; |
| 2188 | } |
| 2189 | |
| 2190 | static void phylink_decode_sgmii_word(struct phylink_link_state *state, |
| 2191 | uint16_t config_reg) |
| 2192 | { |
| 2193 | if (!(config_reg & LPA_SGMII_LINK)) { |
| 2194 | state->link = false; |
| 2195 | return; |
| 2196 | } |
| 2197 | |
| 2198 | switch (config_reg & LPA_SGMII_SPD_MASK) { |
| 2199 | case LPA_SGMII_10: |
| 2200 | state->speed = SPEED_10; |
| 2201 | break; |
| 2202 | case LPA_SGMII_100: |
| 2203 | state->speed = SPEED_100; |
| 2204 | break; |
| 2205 | case LPA_SGMII_1000: |
| 2206 | state->speed = SPEED_1000; |
| 2207 | break; |
| 2208 | default: |
| 2209 | state->link = false; |
| 2210 | return; |
| 2211 | } |
| 2212 | if (config_reg & LPA_SGMII_FULL_DUPLEX) |
| 2213 | state->duplex = DUPLEX_FULL; |
| 2214 | else |
| 2215 | state->duplex = DUPLEX_HALF; |
| 2216 | } |
| 2217 | |
| 2218 | /** |
| 2219 | * phylink_mii_c22_pcs_get_state() - read the MAC PCS state |
| 2220 | * @pcs: a pointer to a &struct mdio_device. |
| 2221 | * @state: a pointer to a &struct phylink_link_state. |
| 2222 | * |
| 2223 | * Helper for MAC PCS supporting the 802.3 clause 22 register set for |
| 2224 | * clause 37 negotiation and/or SGMII control. |
| 2225 | * |
| 2226 | * Read the MAC PCS state from the MII device configured in @config and |
| 2227 | * parse the Clause 37 or Cisco SGMII link partner negotiation word into |
| 2228 | * the phylink @state structure. This is suitable to be directly plugged |
| 2229 | * into the mac_pcs_get_state() member of the struct phylink_mac_ops |
| 2230 | * structure. |
| 2231 | */ |
| 2232 | void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs, |
| 2233 | struct phylink_link_state *state) |
| 2234 | { |
| 2235 | struct mii_bus *bus = pcs->bus; |
| 2236 | int addr = pcs->addr; |
| 2237 | int bmsr, lpa; |
| 2238 | |
| 2239 | bmsr = mdiobus_read(bus, addr, MII_BMSR); |
| 2240 | lpa = mdiobus_read(bus, addr, MII_LPA); |
| 2241 | if (bmsr < 0 || lpa < 0) { |
| 2242 | state->link = false; |
| 2243 | return; |
| 2244 | } |
| 2245 | |
| 2246 | state->link = !!(bmsr & BMSR_LSTATUS); |
| 2247 | state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE); |
| 2248 | if (!state->link) |
| 2249 | return; |
| 2250 | |
| 2251 | switch (state->interface) { |
| 2252 | case PHY_INTERFACE_MODE_1000BASEX: |
| 2253 | phylink_decode_c37_word(state, lpa, SPEED_1000); |
| 2254 | break; |
| 2255 | |
| 2256 | case PHY_INTERFACE_MODE_2500BASEX: |
| 2257 | phylink_decode_c37_word(state, lpa, SPEED_2500); |
| 2258 | break; |
| 2259 | |
| 2260 | case PHY_INTERFACE_MODE_SGMII: |
| 2261 | phylink_decode_sgmii_word(state, lpa); |
| 2262 | break; |
| 2263 | |
| 2264 | default: |
| 2265 | state->link = false; |
| 2266 | break; |
| 2267 | } |
| 2268 | } |
| 2269 | EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state); |
| 2270 | |
| 2271 | /** |
| 2272 | * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS |
| 2273 | * advertisement |
| 2274 | * @pcs: a pointer to a &struct mdio_device. |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2275 | * @interface: the PHY interface mode being configured |
| 2276 | * @advertising: the ethtool advertisement mask |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2277 | * |
| 2278 | * Helper for MAC PCS supporting the 802.3 clause 22 register set for |
| 2279 | * clause 37 negotiation and/or SGMII control. |
| 2280 | * |
| 2281 | * Configure the clause 37 PCS advertisement as specified by @state. This |
| 2282 | * does not trigger a renegotiation; phylink will do that via the |
| 2283 | * mac_an_restart() method of the struct phylink_mac_ops structure. |
| 2284 | * |
| 2285 | * Returns negative error code on failure to configure the advertisement, |
| 2286 | * zero if no change has been made, or one if the advertisement has changed. |
| 2287 | */ |
| 2288 | int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs, |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2289 | phy_interface_t interface, |
| 2290 | const unsigned long *advertising) |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2291 | { |
| 2292 | struct mii_bus *bus = pcs->bus; |
| 2293 | int addr = pcs->addr; |
| 2294 | int val, ret; |
| 2295 | u16 adv; |
| 2296 | |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2297 | switch (interface) { |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2298 | case PHY_INTERFACE_MODE_1000BASEX: |
| 2299 | case PHY_INTERFACE_MODE_2500BASEX: |
| 2300 | adv = ADVERTISE_1000XFULL; |
| 2301 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2302 | advertising)) |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2303 | adv |= ADVERTISE_1000XPAUSE; |
| 2304 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2305 | advertising)) |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2306 | adv |= ADVERTISE_1000XPSE_ASYM; |
| 2307 | |
| 2308 | val = mdiobus_read(bus, addr, MII_ADVERTISE); |
| 2309 | if (val < 0) |
| 2310 | return val; |
| 2311 | |
| 2312 | if (val == adv) |
| 2313 | return 0; |
| 2314 | |
| 2315 | ret = mdiobus_write(bus, addr, MII_ADVERTISE, adv); |
| 2316 | if (ret < 0) |
| 2317 | return ret; |
| 2318 | |
| 2319 | return 1; |
| 2320 | |
| 2321 | case PHY_INTERFACE_MODE_SGMII: |
| 2322 | val = mdiobus_read(bus, addr, MII_ADVERTISE); |
| 2323 | if (val < 0) |
| 2324 | return val; |
| 2325 | |
| 2326 | if (val == 0x0001) |
| 2327 | return 0; |
| 2328 | |
| 2329 | ret = mdiobus_write(bus, addr, MII_ADVERTISE, 0x0001); |
| 2330 | if (ret < 0) |
| 2331 | return ret; |
| 2332 | |
| 2333 | return 1; |
| 2334 | |
| 2335 | default: |
| 2336 | /* Nothing to do for other modes */ |
| 2337 | return 0; |
| 2338 | } |
| 2339 | } |
| 2340 | EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement); |
| 2341 | |
| 2342 | /** |
| 2343 | * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation |
| 2344 | * @pcs: a pointer to a &struct mdio_device. |
| 2345 | * |
| 2346 | * Helper for MAC PCS supporting the 802.3 clause 22 register set for |
| 2347 | * clause 37 negotiation. |
| 2348 | * |
| 2349 | * Restart the clause 37 negotiation with the link partner. This is |
| 2350 | * suitable to be directly plugged into the mac_pcs_get_state() member |
| 2351 | * of the struct phylink_mac_ops structure. |
| 2352 | */ |
| 2353 | void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs) |
| 2354 | { |
| 2355 | struct mii_bus *bus = pcs->bus; |
| 2356 | int val, addr = pcs->addr; |
| 2357 | |
| 2358 | val = mdiobus_read(bus, addr, MII_BMCR); |
| 2359 | if (val >= 0) { |
| 2360 | val |= BMCR_ANRESTART; |
| 2361 | |
| 2362 | mdiobus_write(bus, addr, MII_BMCR, val); |
| 2363 | } |
| 2364 | } |
| 2365 | EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart); |
| 2366 | |
Russell King | b8679ef | 2020-03-17 14:52:41 +0000 | [diff] [blame] | 2367 | void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs, |
| 2368 | struct phylink_link_state *state) |
| 2369 | { |
| 2370 | struct mii_bus *bus = pcs->bus; |
| 2371 | int addr = pcs->addr; |
| 2372 | int stat; |
| 2373 | |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 2374 | stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1); |
Russell King | b8679ef | 2020-03-17 14:52:41 +0000 | [diff] [blame] | 2375 | if (stat < 0) { |
| 2376 | state->link = false; |
| 2377 | return; |
| 2378 | } |
| 2379 | |
| 2380 | state->link = !!(stat & MDIO_STAT1_LSTATUS); |
| 2381 | if (!state->link) |
| 2382 | return; |
| 2383 | |
| 2384 | switch (state->interface) { |
| 2385 | case PHY_INTERFACE_MODE_10GBASER: |
| 2386 | state->speed = SPEED_10000; |
| 2387 | state->duplex = DUPLEX_FULL; |
| 2388 | break; |
| 2389 | |
| 2390 | default: |
| 2391 | break; |
| 2392 | } |
| 2393 | } |
| 2394 | EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state); |
| 2395 | |
Andrew Lunn | 5f85757 | 2019-01-21 19:10:19 +0100 | [diff] [blame] | 2396 | MODULE_LICENSE("GPL v2"); |