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