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