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