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