root/drivers/net/phy/phylink.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. phylink_set_port_modes
  2. phylink_is_empty_linkmode
  3. phylink_an_mode_str
  4. phylink_validate
  5. phylink_parse_fixedlink
  6. phylink_parse_mode
  7. phylink_mac_config
  8. phylink_mac_config_up
  9. phylink_mac_an_restart
  10. phylink_get_mac_state
  11. phylink_get_fixed_state
  12. phylink_resolve_flow
  13. phylink_pause_to_str
  14. phylink_mac_link_up
  15. phylink_mac_link_down
  16. phylink_resolve
  17. phylink_run_resolve
  18. phylink_run_resolve_and_disable
  19. phylink_fixed_poll
  20. phylink_register_sfp
  21. phylink_create
  22. phylink_destroy
  23. phylink_phy_change
  24. phylink_bringup_phy
  25. __phylink_connect_phy
  26. phylink_connect_phy
  27. phylink_of_phy_connect
  28. phylink_disconnect_phy
  29. phylink_fixed_state_cb
  30. phylink_mac_change
  31. phylink_link_handler
  32. phylink_start
  33. phylink_stop
  34. phylink_ethtool_get_wol
  35. phylink_ethtool_set_wol
  36. phylink_merge_link_mode
  37. phylink_get_ksettings
  38. phylink_ethtool_ksettings_get
  39. phylink_ethtool_ksettings_set
  40. phylink_ethtool_nway_reset
  41. phylink_ethtool_get_pauseparam
  42. phylink_ethtool_set_pauseparam
  43. phylink_get_eee_err
  44. phylink_init_eee
  45. phylink_ethtool_get_eee
  46. phylink_ethtool_set_eee
  47. phylink_mii_emul_read
  48. phylink_phy_read
  49. phylink_phy_write
  50. phylink_mii_read
  51. phylink_mii_write
  52. phylink_mii_ioctl
  53. phylink_sfp_attach
  54. phylink_sfp_detach
  55. phylink_sfp_module_insert
  56. phylink_sfp_link_down
  57. phylink_sfp_link_up
  58. phylink_sfp_connect_phy
  59. phylink_sfp_disconnect_phy
  60. phylink_helper_basex_speed

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

/* [<][>][^][v][top][bottom][index][help] */