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