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