1 /* Framework for configuring and reading PHY devices 2 * Based on code in sungem_phy.c and gianfar_phy.c 3 * 4 * Author: Andy Fleming 5 * 6 * Copyright (c) 2004 Freescale Semiconductor, Inc. 7 * Copyright (c) 2006, 2007 Maciej W. Rozycki 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by the 11 * Free Software Foundation; either version 2 of the License, or (at your 12 * option) any later version. 13 * 14 */ 15 16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 17 18 #include <linux/kernel.h> 19 #include <linux/string.h> 20 #include <linux/errno.h> 21 #include <linux/unistd.h> 22 #include <linux/interrupt.h> 23 #include <linux/delay.h> 24 #include <linux/netdevice.h> 25 #include <linux/etherdevice.h> 26 #include <linux/skbuff.h> 27 #include <linux/mm.h> 28 #include <linux/module.h> 29 #include <linux/mii.h> 30 #include <linux/ethtool.h> 31 #include <linux/phy.h> 32 #include <linux/phy_led_triggers.h> 33 #include <linux/workqueue.h> 34 #include <linux/mdio.h> 35 #include <linux/io.h> 36 #include <linux/uaccess.h> 37 #include <linux/atomic.h> 38 39 #include <asm/irq.h> 40 41 #define PHY_STATE_STR(_state) \ 42 case PHY_##_state: \ 43 return __stringify(_state); \ 44 45 static const char *phy_state_to_str(enum phy_state st) 46 { 47 switch (st) { 48 PHY_STATE_STR(DOWN) 49 PHY_STATE_STR(STARTING) 50 PHY_STATE_STR(READY) 51 PHY_STATE_STR(PENDING) 52 PHY_STATE_STR(UP) 53 PHY_STATE_STR(AN) 54 PHY_STATE_STR(RUNNING) 55 PHY_STATE_STR(NOLINK) 56 PHY_STATE_STR(FORCING) 57 PHY_STATE_STR(CHANGELINK) 58 PHY_STATE_STR(HALTED) 59 PHY_STATE_STR(RESUMING) 60 } 61 62 return NULL; 63 } 64 65 66 /** 67 * phy_print_status - Convenience function to print out the current phy status 68 * @phydev: the phy_device struct 69 */ 70 void phy_print_status(struct phy_device *phydev) 71 { 72 if (phydev->link) { 73 netdev_info(phydev->attached_dev, 74 "Link is Up - %s/%s - flow control %s\n", 75 phy_speed_to_str(phydev->speed), 76 phy_duplex_to_str(phydev->duplex), 77 phydev->pause ? "rx/tx" : "off"); 78 } else { 79 netdev_info(phydev->attached_dev, "Link is Down\n"); 80 } 81 } 82 EXPORT_SYMBOL(phy_print_status); 83 84 /** 85 * phy_clear_interrupt - Ack the phy device's interrupt 86 * @phydev: the phy_device struct 87 * 88 * If the @phydev driver has an ack_interrupt function, call it to 89 * ack and clear the phy device's interrupt. 90 * 91 * Returns 0 on success or < 0 on error. 92 */ 93 static int phy_clear_interrupt(struct phy_device *phydev) 94 { 95 if (phydev->drv->ack_interrupt) 96 return phydev->drv->ack_interrupt(phydev); 97 98 return 0; 99 } 100 101 /** 102 * phy_config_interrupt - configure the PHY device for the requested interrupts 103 * @phydev: the phy_device struct 104 * @interrupts: interrupt flags to configure for this @phydev 105 * 106 * Returns 0 on success or < 0 on error. 107 */ 108 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts) 109 { 110 phydev->interrupts = interrupts; 111 if (phydev->drv->config_intr) 112 return phydev->drv->config_intr(phydev); 113 114 return 0; 115 } 116 117 /** 118 * phy_restart_aneg - restart auto-negotiation 119 * @phydev: target phy_device struct 120 * 121 * Restart the autonegotiation on @phydev. Returns >= 0 on success or 122 * negative errno on error. 123 */ 124 int phy_restart_aneg(struct phy_device *phydev) 125 { 126 int ret; 127 128 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0))) 129 ret = genphy_c45_restart_aneg(phydev); 130 else 131 ret = genphy_restart_aneg(phydev); 132 133 return ret; 134 } 135 EXPORT_SYMBOL_GPL(phy_restart_aneg); 136 137 /** 138 * phy_aneg_done - return auto-negotiation status 139 * @phydev: target phy_device struct 140 * 141 * Description: Return the auto-negotiation status from this @phydev 142 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation 143 * is still pending. 144 */ 145 int phy_aneg_done(struct phy_device *phydev) 146 { 147 if (phydev->drv && phydev->drv->aneg_done) 148 return phydev->drv->aneg_done(phydev); 149 150 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not 151 * implement Clause 22 registers 152 */ 153 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0))) 154 return -EINVAL; 155 156 return genphy_aneg_done(phydev); 157 } 158 EXPORT_SYMBOL(phy_aneg_done); 159 160 /** 161 * phy_find_valid - find a PHY setting that matches the requested parameters 162 * @speed: desired speed 163 * @duplex: desired duplex 164 * @supported: mask of supported link modes 165 * 166 * Locate a supported phy setting that is, in priority order: 167 * - an exact match for the specified speed and duplex mode 168 * - a match for the specified speed, or slower speed 169 * - the slowest supported speed 170 * Returns the matched phy_setting entry, or %NULL if no supported phy 171 * settings were found. 172 */ 173 static const struct phy_setting * 174 phy_find_valid(int speed, int duplex, u32 supported) 175 { 176 unsigned long mask = supported; 177 178 return phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, false); 179 } 180 181 /** 182 * phy_supported_speeds - return all speeds currently supported by a phy device 183 * @phy: The phy device to return supported speeds of. 184 * @speeds: buffer to store supported speeds in. 185 * @size: size of speeds buffer. 186 * 187 * Description: Returns the number of supported speeds, and fills the speeds 188 * buffer with the supported speeds. If speeds buffer is too small to contain 189 * all currently supported speeds, will return as many speeds as can fit. 190 */ 191 unsigned int phy_supported_speeds(struct phy_device *phy, 192 unsigned int *speeds, 193 unsigned int size) 194 { 195 unsigned long supported = phy->supported; 196 197 return phy_speeds(speeds, size, &supported, BITS_PER_LONG); 198 } 199 200 /** 201 * phy_check_valid - check if there is a valid PHY setting which matches 202 * speed, duplex, and feature mask 203 * @speed: speed to match 204 * @duplex: duplex to match 205 * @features: A mask of the valid settings 206 * 207 * Description: Returns true if there is a valid setting, false otherwise. 208 */ 209 static inline bool phy_check_valid(int speed, int duplex, u32 features) 210 { 211 unsigned long mask = features; 212 213 return !!phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, true); 214 } 215 216 /** 217 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex 218 * @phydev: the target phy_device struct 219 * 220 * Description: Make sure the PHY is set to supported speeds and 221 * duplexes. Drop down by one in this order: 1000/FULL, 222 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. 223 */ 224 static void phy_sanitize_settings(struct phy_device *phydev) 225 { 226 const struct phy_setting *setting; 227 u32 features = phydev->supported; 228 229 /* Sanitize settings based on PHY capabilities */ 230 if ((features & SUPPORTED_Autoneg) == 0) 231 phydev->autoneg = AUTONEG_DISABLE; 232 233 setting = phy_find_valid(phydev->speed, phydev->duplex, features); 234 if (setting) { 235 phydev->speed = setting->speed; 236 phydev->duplex = setting->duplex; 237 } else { 238 /* We failed to find anything (no supported speeds?) */ 239 phydev->speed = SPEED_UNKNOWN; 240 phydev->duplex = DUPLEX_UNKNOWN; 241 } 242 } 243 244 /** 245 * phy_ethtool_sset - generic ethtool sset function, handles all the details 246 * @phydev: target phy_device struct 247 * @cmd: ethtool_cmd 248 * 249 * A few notes about parameter checking: 250 * 251 * - We don't set port or transceiver, so we don't care what they 252 * were set to. 253 * - phy_start_aneg() will make sure forced settings are sane, and 254 * choose the next best ones from the ones selected, so we don't 255 * care if ethtool tries to give us bad values. 256 */ 257 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd) 258 { 259 u32 speed = ethtool_cmd_speed(cmd); 260 261 if (cmd->phy_address != phydev->mdio.addr) 262 return -EINVAL; 263 264 /* We make sure that we don't pass unsupported values in to the PHY */ 265 cmd->advertising &= phydev->supported; 266 267 /* Verify the settings we care about. */ 268 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE) 269 return -EINVAL; 270 271 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0) 272 return -EINVAL; 273 274 if (cmd->autoneg == AUTONEG_DISABLE && 275 ((speed != SPEED_1000 && 276 speed != SPEED_100 && 277 speed != SPEED_10) || 278 (cmd->duplex != DUPLEX_HALF && 279 cmd->duplex != DUPLEX_FULL))) 280 return -EINVAL; 281 282 phydev->autoneg = cmd->autoneg; 283 284 phydev->speed = speed; 285 286 phydev->advertising = cmd->advertising; 287 288 if (AUTONEG_ENABLE == cmd->autoneg) 289 phydev->advertising |= ADVERTISED_Autoneg; 290 else 291 phydev->advertising &= ~ADVERTISED_Autoneg; 292 293 phydev->duplex = cmd->duplex; 294 295 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl; 296 297 /* Restart the PHY */ 298 phy_start_aneg(phydev); 299 300 return 0; 301 } 302 EXPORT_SYMBOL(phy_ethtool_sset); 303 304 int phy_ethtool_ksettings_set(struct phy_device *phydev, 305 const struct ethtool_link_ksettings *cmd) 306 { 307 u8 autoneg = cmd->base.autoneg; 308 u8 duplex = cmd->base.duplex; 309 u32 speed = cmd->base.speed; 310 u32 advertising; 311 312 if (cmd->base.phy_address != phydev->mdio.addr) 313 return -EINVAL; 314 315 ethtool_convert_link_mode_to_legacy_u32(&advertising, 316 cmd->link_modes.advertising); 317 318 /* We make sure that we don't pass unsupported values in to the PHY */ 319 advertising &= phydev->supported; 320 321 /* Verify the settings we care about. */ 322 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE) 323 return -EINVAL; 324 325 if (autoneg == AUTONEG_ENABLE && advertising == 0) 326 return -EINVAL; 327 328 if (autoneg == AUTONEG_DISABLE && 329 ((speed != SPEED_1000 && 330 speed != SPEED_100 && 331 speed != SPEED_10) || 332 (duplex != DUPLEX_HALF && 333 duplex != DUPLEX_FULL))) 334 return -EINVAL; 335 336 phydev->autoneg = autoneg; 337 338 phydev->speed = speed; 339 340 phydev->advertising = advertising; 341 342 if (autoneg == AUTONEG_ENABLE) 343 phydev->advertising |= ADVERTISED_Autoneg; 344 else 345 phydev->advertising &= ~ADVERTISED_Autoneg; 346 347 phydev->duplex = duplex; 348 349 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl; 350 351 /* Restart the PHY */ 352 phy_start_aneg(phydev); 353 354 return 0; 355 } 356 EXPORT_SYMBOL(phy_ethtool_ksettings_set); 357 358 void phy_ethtool_ksettings_get(struct phy_device *phydev, 359 struct ethtool_link_ksettings *cmd) 360 { 361 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 362 phydev->supported); 363 364 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 365 phydev->advertising); 366 367 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising, 368 phydev->lp_advertising); 369 370 cmd->base.speed = phydev->speed; 371 cmd->base.duplex = phydev->duplex; 372 if (phydev->interface == PHY_INTERFACE_MODE_MOCA) 373 cmd->base.port = PORT_BNC; 374 else 375 cmd->base.port = PORT_MII; 376 377 cmd->base.phy_address = phydev->mdio.addr; 378 cmd->base.autoneg = phydev->autoneg; 379 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl; 380 cmd->base.eth_tp_mdix = phydev->mdix; 381 } 382 EXPORT_SYMBOL(phy_ethtool_ksettings_get); 383 384 /** 385 * phy_mii_ioctl - generic PHY MII ioctl interface 386 * @phydev: the phy_device struct 387 * @ifr: &struct ifreq for socket ioctl's 388 * @cmd: ioctl cmd to execute 389 * 390 * Note that this function is currently incompatible with the 391 * PHYCONTROL layer. It changes registers without regard to 392 * current state. Use at own risk. 393 */ 394 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd) 395 { 396 struct mii_ioctl_data *mii_data = if_mii(ifr); 397 u16 val = mii_data->val_in; 398 bool change_autoneg = false; 399 400 switch (cmd) { 401 case SIOCGMIIPHY: 402 mii_data->phy_id = phydev->mdio.addr; 403 /* fall through */ 404 405 case SIOCGMIIREG: 406 mii_data->val_out = mdiobus_read(phydev->mdio.bus, 407 mii_data->phy_id, 408 mii_data->reg_num); 409 return 0; 410 411 case SIOCSMIIREG: 412 if (mii_data->phy_id == phydev->mdio.addr) { 413 switch (mii_data->reg_num) { 414 case MII_BMCR: 415 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) { 416 if (phydev->autoneg == AUTONEG_ENABLE) 417 change_autoneg = true; 418 phydev->autoneg = AUTONEG_DISABLE; 419 if (val & BMCR_FULLDPLX) 420 phydev->duplex = DUPLEX_FULL; 421 else 422 phydev->duplex = DUPLEX_HALF; 423 if (val & BMCR_SPEED1000) 424 phydev->speed = SPEED_1000; 425 else if (val & BMCR_SPEED100) 426 phydev->speed = SPEED_100; 427 else phydev->speed = SPEED_10; 428 } 429 else { 430 if (phydev->autoneg == AUTONEG_DISABLE) 431 change_autoneg = true; 432 phydev->autoneg = AUTONEG_ENABLE; 433 } 434 break; 435 case MII_ADVERTISE: 436 phydev->advertising = mii_adv_to_ethtool_adv_t(val); 437 change_autoneg = true; 438 break; 439 default: 440 /* do nothing */ 441 break; 442 } 443 } 444 445 mdiobus_write(phydev->mdio.bus, mii_data->phy_id, 446 mii_data->reg_num, val); 447 448 if (mii_data->phy_id == phydev->mdio.addr && 449 mii_data->reg_num == MII_BMCR && 450 val & BMCR_RESET) 451 return phy_init_hw(phydev); 452 453 if (change_autoneg) 454 return phy_start_aneg(phydev); 455 456 return 0; 457 458 case SIOCSHWTSTAMP: 459 if (phydev->drv && phydev->drv->hwtstamp) 460 return phydev->drv->hwtstamp(phydev, ifr); 461 /* fall through */ 462 463 default: 464 return -EOPNOTSUPP; 465 } 466 } 467 EXPORT_SYMBOL(phy_mii_ioctl); 468 469 /** 470 * phy_start_aneg_priv - start auto-negotiation for this PHY device 471 * @phydev: the phy_device struct 472 * @sync: indicate whether we should wait for the workqueue cancelation 473 * 474 * Description: Sanitizes the settings (if we're not autonegotiating 475 * them), and then calls the driver's config_aneg function. 476 * If the PHYCONTROL Layer is operating, we change the state to 477 * reflect the beginning of Auto-negotiation or forcing. 478 */ 479 static int phy_start_aneg_priv(struct phy_device *phydev, bool sync) 480 { 481 bool trigger = 0; 482 int err; 483 484 if (!phydev->drv) 485 return -EIO; 486 487 mutex_lock(&phydev->lock); 488 489 if (AUTONEG_DISABLE == phydev->autoneg) 490 phy_sanitize_settings(phydev); 491 492 /* Invalidate LP advertising flags */ 493 phydev->lp_advertising = 0; 494 495 err = phydev->drv->config_aneg(phydev); 496 if (err < 0) 497 goto out_unlock; 498 499 if (phydev->state != PHY_HALTED) { 500 if (AUTONEG_ENABLE == phydev->autoneg) { 501 phydev->state = PHY_AN; 502 phydev->link_timeout = PHY_AN_TIMEOUT; 503 } else { 504 phydev->state = PHY_FORCING; 505 phydev->link_timeout = PHY_FORCE_TIMEOUT; 506 } 507 } 508 509 /* Re-schedule a PHY state machine to check PHY status because 510 * negotiation may already be done and aneg interrupt may not be 511 * generated. 512 */ 513 if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) { 514 err = phy_aneg_done(phydev); 515 if (err > 0) { 516 trigger = true; 517 err = 0; 518 } 519 } 520 521 out_unlock: 522 mutex_unlock(&phydev->lock); 523 524 if (trigger) 525 phy_trigger_machine(phydev, sync); 526 527 return err; 528 } 529 530 /** 531 * phy_start_aneg - start auto-negotiation for this PHY device 532 * @phydev: the phy_device struct 533 * 534 * Description: Sanitizes the settings (if we're not autonegotiating 535 * them), and then calls the driver's config_aneg function. 536 * If the PHYCONTROL Layer is operating, we change the state to 537 * reflect the beginning of Auto-negotiation or forcing. 538 */ 539 int phy_start_aneg(struct phy_device *phydev) 540 { 541 return phy_start_aneg_priv(phydev, true); 542 } 543 EXPORT_SYMBOL(phy_start_aneg); 544 545 /** 546 * phy_start_machine - start PHY state machine tracking 547 * @phydev: the phy_device struct 548 * 549 * Description: The PHY infrastructure can run a state machine 550 * which tracks whether the PHY is starting up, negotiating, 551 * etc. This function starts the delayed workqueue which tracks 552 * the state of the PHY. If you want to maintain your own state machine, 553 * do not call this function. 554 */ 555 void phy_start_machine(struct phy_device *phydev) 556 { 557 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ); 558 } 559 EXPORT_SYMBOL_GPL(phy_start_machine); 560 561 /** 562 * phy_trigger_machine - trigger the state machine to run 563 * 564 * @phydev: the phy_device struct 565 * @sync: indicate whether we should wait for the workqueue cancelation 566 * 567 * Description: There has been a change in state which requires that the 568 * state machine runs. 569 */ 570 571 void phy_trigger_machine(struct phy_device *phydev, bool sync) 572 { 573 if (sync) 574 cancel_delayed_work_sync(&phydev->state_queue); 575 else 576 cancel_delayed_work(&phydev->state_queue); 577 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0); 578 } 579 580 /** 581 * phy_stop_machine - stop the PHY state machine tracking 582 * @phydev: target phy_device struct 583 * 584 * Description: Stops the state machine delayed workqueue, sets the 585 * state to UP (unless it wasn't up yet). This function must be 586 * called BEFORE phy_detach. 587 */ 588 void phy_stop_machine(struct phy_device *phydev) 589 { 590 cancel_delayed_work_sync(&phydev->state_queue); 591 592 mutex_lock(&phydev->lock); 593 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED) 594 phydev->state = PHY_UP; 595 mutex_unlock(&phydev->lock); 596 597 /* Now we can run the state machine synchronously */ 598 phy_state_machine(&phydev->state_queue.work); 599 } 600 601 /** 602 * phy_error - enter HALTED state for this PHY device 603 * @phydev: target phy_device struct 604 * 605 * Moves the PHY to the HALTED state in response to a read 606 * or write error, and tells the controller the link is down. 607 * Must not be called from interrupt context, or while the 608 * phydev->lock is held. 609 */ 610 static void phy_error(struct phy_device *phydev) 611 { 612 mutex_lock(&phydev->lock); 613 phydev->state = PHY_HALTED; 614 mutex_unlock(&phydev->lock); 615 616 phy_trigger_machine(phydev, false); 617 } 618 619 /** 620 * phy_interrupt - PHY interrupt handler 621 * @irq: interrupt line 622 * @phy_dat: phy_device pointer 623 * 624 * Description: When a PHY interrupt occurs, the handler disables 625 * interrupts, and uses phy_change to handle the interrupt. 626 */ 627 static irqreturn_t phy_interrupt(int irq, void *phy_dat) 628 { 629 struct phy_device *phydev = phy_dat; 630 631 if (PHY_HALTED == phydev->state) 632 return IRQ_NONE; /* It can't be ours. */ 633 634 disable_irq_nosync(irq); 635 atomic_inc(&phydev->irq_disable); 636 637 phy_change(phydev); 638 639 return IRQ_HANDLED; 640 } 641 642 /** 643 * phy_enable_interrupts - Enable the interrupts from the PHY side 644 * @phydev: target phy_device struct 645 */ 646 static int phy_enable_interrupts(struct phy_device *phydev) 647 { 648 int err = phy_clear_interrupt(phydev); 649 650 if (err < 0) 651 return err; 652 653 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); 654 } 655 656 /** 657 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side 658 * @phydev: target phy_device struct 659 */ 660 static int phy_disable_interrupts(struct phy_device *phydev) 661 { 662 int err; 663 664 /* Disable PHY interrupts */ 665 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); 666 if (err) 667 goto phy_err; 668 669 /* Clear the interrupt */ 670 err = phy_clear_interrupt(phydev); 671 if (err) 672 goto phy_err; 673 674 return 0; 675 676 phy_err: 677 phy_error(phydev); 678 679 return err; 680 } 681 682 /** 683 * phy_start_interrupts - request and enable interrupts for a PHY device 684 * @phydev: target phy_device struct 685 * 686 * Description: Request the interrupt for the given PHY. 687 * If this fails, then we set irq to PHY_POLL. 688 * Otherwise, we enable the interrupts in the PHY. 689 * This should only be called with a valid IRQ number. 690 * Returns 0 on success or < 0 on error. 691 */ 692 int phy_start_interrupts(struct phy_device *phydev) 693 { 694 atomic_set(&phydev->irq_disable, 0); 695 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt, 696 IRQF_ONESHOT | IRQF_SHARED, 697 phydev_name(phydev), phydev) < 0) { 698 pr_warn("%s: Can't get IRQ %d (PHY)\n", 699 phydev->mdio.bus->name, phydev->irq); 700 phydev->irq = PHY_POLL; 701 return 0; 702 } 703 704 return phy_enable_interrupts(phydev); 705 } 706 EXPORT_SYMBOL(phy_start_interrupts); 707 708 /** 709 * phy_stop_interrupts - disable interrupts from a PHY device 710 * @phydev: target phy_device struct 711 */ 712 int phy_stop_interrupts(struct phy_device *phydev) 713 { 714 int err = phy_disable_interrupts(phydev); 715 716 if (err) 717 phy_error(phydev); 718 719 free_irq(phydev->irq, phydev); 720 721 /* If work indeed has been cancelled, disable_irq() will have 722 * been left unbalanced from phy_interrupt() and enable_irq() 723 * has to be called so that other devices on the line work. 724 */ 725 while (atomic_dec_return(&phydev->irq_disable) >= 0) 726 enable_irq(phydev->irq); 727 728 return err; 729 } 730 EXPORT_SYMBOL(phy_stop_interrupts); 731 732 /** 733 * phy_change - Called by the phy_interrupt to handle PHY changes 734 * @phydev: phy_device struct that interrupted 735 */ 736 void phy_change(struct phy_device *phydev) 737 { 738 if (phy_interrupt_is_valid(phydev)) { 739 if (phydev->drv->did_interrupt && 740 !phydev->drv->did_interrupt(phydev)) 741 goto ignore; 742 743 if (phy_disable_interrupts(phydev)) 744 goto phy_err; 745 } 746 747 mutex_lock(&phydev->lock); 748 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state)) 749 phydev->state = PHY_CHANGELINK; 750 mutex_unlock(&phydev->lock); 751 752 if (phy_interrupt_is_valid(phydev)) { 753 atomic_dec(&phydev->irq_disable); 754 enable_irq(phydev->irq); 755 756 /* Reenable interrupts */ 757 if (PHY_HALTED != phydev->state && 758 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED)) 759 goto irq_enable_err; 760 } 761 762 /* reschedule state queue work to run as soon as possible */ 763 phy_trigger_machine(phydev, true); 764 return; 765 766 ignore: 767 atomic_dec(&phydev->irq_disable); 768 enable_irq(phydev->irq); 769 return; 770 771 irq_enable_err: 772 disable_irq(phydev->irq); 773 atomic_inc(&phydev->irq_disable); 774 phy_err: 775 phy_error(phydev); 776 } 777 778 /** 779 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes 780 * @work: work_struct that describes the work to be done 781 */ 782 void phy_change_work(struct work_struct *work) 783 { 784 struct phy_device *phydev = 785 container_of(work, struct phy_device, phy_queue); 786 787 phy_change(phydev); 788 } 789 790 /** 791 * phy_stop - Bring down the PHY link, and stop checking the status 792 * @phydev: target phy_device struct 793 */ 794 void phy_stop(struct phy_device *phydev) 795 { 796 mutex_lock(&phydev->lock); 797 798 if (PHY_HALTED == phydev->state) 799 goto out_unlock; 800 801 if (phy_interrupt_is_valid(phydev)) { 802 /* Disable PHY Interrupts */ 803 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); 804 805 /* Clear any pending interrupts */ 806 phy_clear_interrupt(phydev); 807 } 808 809 phydev->state = PHY_HALTED; 810 811 out_unlock: 812 mutex_unlock(&phydev->lock); 813 814 /* Cannot call flush_scheduled_work() here as desired because 815 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change() 816 * will not reenable interrupts. 817 */ 818 } 819 EXPORT_SYMBOL(phy_stop); 820 821 /** 822 * phy_start - start or restart a PHY device 823 * @phydev: target phy_device struct 824 * 825 * Description: Indicates the attached device's readiness to 826 * handle PHY-related work. Used during startup to start the 827 * PHY, and after a call to phy_stop() to resume operation. 828 * Also used to indicate the MDIO bus has cleared an error 829 * condition. 830 */ 831 void phy_start(struct phy_device *phydev) 832 { 833 bool do_resume = false; 834 int err = 0; 835 836 mutex_lock(&phydev->lock); 837 838 switch (phydev->state) { 839 case PHY_STARTING: 840 phydev->state = PHY_PENDING; 841 break; 842 case PHY_READY: 843 phydev->state = PHY_UP; 844 break; 845 case PHY_HALTED: 846 /* make sure interrupts are re-enabled for the PHY */ 847 if (phydev->irq != PHY_POLL) { 848 err = phy_enable_interrupts(phydev); 849 if (err < 0) 850 break; 851 } 852 853 phydev->state = PHY_RESUMING; 854 do_resume = true; 855 break; 856 default: 857 break; 858 } 859 mutex_unlock(&phydev->lock); 860 861 /* if phy was suspended, bring the physical link up again */ 862 if (do_resume) 863 phy_resume(phydev); 864 865 phy_trigger_machine(phydev, true); 866 } 867 EXPORT_SYMBOL(phy_start); 868 869 static void phy_link_up(struct phy_device *phydev) 870 { 871 phydev->phy_link_change(phydev, true, true); 872 phy_led_trigger_change_speed(phydev); 873 } 874 875 static void phy_link_down(struct phy_device *phydev, bool do_carrier) 876 { 877 phydev->phy_link_change(phydev, false, do_carrier); 878 phy_led_trigger_change_speed(phydev); 879 } 880 881 /** 882 * phy_state_machine - Handle the state machine 883 * @work: work_struct that describes the work to be done 884 */ 885 void phy_state_machine(struct work_struct *work) 886 { 887 struct delayed_work *dwork = to_delayed_work(work); 888 struct phy_device *phydev = 889 container_of(dwork, struct phy_device, state_queue); 890 bool needs_aneg = false, do_suspend = false; 891 enum phy_state old_state; 892 int err = 0; 893 int old_link; 894 895 mutex_lock(&phydev->lock); 896 897 old_state = phydev->state; 898 899 if (phydev->drv && phydev->drv->link_change_notify) 900 phydev->drv->link_change_notify(phydev); 901 902 switch (phydev->state) { 903 case PHY_DOWN: 904 case PHY_STARTING: 905 case PHY_READY: 906 case PHY_PENDING: 907 break; 908 case PHY_UP: 909 needs_aneg = true; 910 911 phydev->link_timeout = PHY_AN_TIMEOUT; 912 913 break; 914 case PHY_AN: 915 err = phy_read_status(phydev); 916 if (err < 0) 917 break; 918 919 /* If the link is down, give up on negotiation for now */ 920 if (!phydev->link) { 921 phydev->state = PHY_NOLINK; 922 phy_link_down(phydev, true); 923 break; 924 } 925 926 /* Check if negotiation is done. Break if there's an error */ 927 err = phy_aneg_done(phydev); 928 if (err < 0) 929 break; 930 931 /* If AN is done, we're running */ 932 if (err > 0) { 933 phydev->state = PHY_RUNNING; 934 phy_link_up(phydev); 935 } else if (0 == phydev->link_timeout--) 936 needs_aneg = true; 937 break; 938 case PHY_NOLINK: 939 if (phy_interrupt_is_valid(phydev)) 940 break; 941 942 err = phy_read_status(phydev); 943 if (err) 944 break; 945 946 if (phydev->link) { 947 if (AUTONEG_ENABLE == phydev->autoneg) { 948 err = phy_aneg_done(phydev); 949 if (err < 0) 950 break; 951 952 if (!err) { 953 phydev->state = PHY_AN; 954 phydev->link_timeout = PHY_AN_TIMEOUT; 955 break; 956 } 957 } 958 phydev->state = PHY_RUNNING; 959 phy_link_up(phydev); 960 } 961 break; 962 case PHY_FORCING: 963 err = genphy_update_link(phydev); 964 if (err) 965 break; 966 967 if (phydev->link) { 968 phydev->state = PHY_RUNNING; 969 phy_link_up(phydev); 970 } else { 971 if (0 == phydev->link_timeout--) 972 needs_aneg = true; 973 phy_link_down(phydev, false); 974 } 975 break; 976 case PHY_RUNNING: 977 /* Only register a CHANGE if we are polling and link changed 978 * since latest checking. 979 */ 980 if (phydev->irq == PHY_POLL) { 981 old_link = phydev->link; 982 err = phy_read_status(phydev); 983 if (err) 984 break; 985 986 if (old_link != phydev->link) 987 phydev->state = PHY_CHANGELINK; 988 } 989 /* 990 * Failsafe: check that nobody set phydev->link=0 between two 991 * poll cycles, otherwise we won't leave RUNNING state as long 992 * as link remains down. 993 */ 994 if (!phydev->link && phydev->state == PHY_RUNNING) { 995 phydev->state = PHY_CHANGELINK; 996 phydev_err(phydev, "no link in PHY_RUNNING\n"); 997 } 998 break; 999 case PHY_CHANGELINK: 1000 err = phy_read_status(phydev); 1001 if (err) 1002 break; 1003 1004 if (phydev->link) { 1005 phydev->state = PHY_RUNNING; 1006 phy_link_up(phydev); 1007 } else { 1008 phydev->state = PHY_NOLINK; 1009 phy_link_down(phydev, true); 1010 } 1011 1012 if (phy_interrupt_is_valid(phydev)) 1013 err = phy_config_interrupt(phydev, 1014 PHY_INTERRUPT_ENABLED); 1015 break; 1016 case PHY_HALTED: 1017 if (phydev->link) { 1018 phydev->link = 0; 1019 phy_link_down(phydev, true); 1020 do_suspend = true; 1021 } 1022 break; 1023 case PHY_RESUMING: 1024 if (AUTONEG_ENABLE == phydev->autoneg) { 1025 err = phy_aneg_done(phydev); 1026 if (err < 0) 1027 break; 1028 1029 /* err > 0 if AN is done. 1030 * Otherwise, it's 0, and we're still waiting for AN 1031 */ 1032 if (err > 0) { 1033 err = phy_read_status(phydev); 1034 if (err) 1035 break; 1036 1037 if (phydev->link) { 1038 phydev->state = PHY_RUNNING; 1039 phy_link_up(phydev); 1040 } else { 1041 phydev->state = PHY_NOLINK; 1042 phy_link_down(phydev, false); 1043 } 1044 } else { 1045 phydev->state = PHY_AN; 1046 phydev->link_timeout = PHY_AN_TIMEOUT; 1047 } 1048 } else { 1049 err = phy_read_status(phydev); 1050 if (err) 1051 break; 1052 1053 if (phydev->link) { 1054 phydev->state = PHY_RUNNING; 1055 phy_link_up(phydev); 1056 } else { 1057 phydev->state = PHY_NOLINK; 1058 phy_link_down(phydev, false); 1059 } 1060 } 1061 break; 1062 } 1063 1064 mutex_unlock(&phydev->lock); 1065 1066 if (needs_aneg) 1067 err = phy_start_aneg_priv(phydev, false); 1068 else if (do_suspend) 1069 phy_suspend(phydev); 1070 1071 if (err < 0) 1072 phy_error(phydev); 1073 1074 if (old_state != phydev->state) 1075 phydev_dbg(phydev, "PHY state change %s -> %s\n", 1076 phy_state_to_str(old_state), 1077 phy_state_to_str(phydev->state)); 1078 1079 /* Only re-schedule a PHY state machine change if we are polling the 1080 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving 1081 * between states from phy_mac_interrupt() 1082 */ 1083 if (phydev->irq == PHY_POLL) 1084 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 1085 PHY_STATE_TIME * HZ); 1086 } 1087 1088 /** 1089 * phy_mac_interrupt - MAC says the link has changed 1090 * @phydev: phy_device struct with changed link 1091 * @new_link: Link is Up/Down. 1092 * 1093 * Description: The MAC layer is able indicate there has been a change 1094 * in the PHY link status. Set the new link status, and trigger the 1095 * state machine, work a work queue. 1096 */ 1097 void phy_mac_interrupt(struct phy_device *phydev, int new_link) 1098 { 1099 phydev->link = new_link; 1100 1101 /* Trigger a state machine change */ 1102 queue_work(system_power_efficient_wq, &phydev->phy_queue); 1103 } 1104 EXPORT_SYMBOL(phy_mac_interrupt); 1105 1106 /** 1107 * phy_init_eee - init and check the EEE feature 1108 * @phydev: target phy_device struct 1109 * @clk_stop_enable: PHY may stop the clock during LPI 1110 * 1111 * Description: it checks if the Energy-Efficient Ethernet (EEE) 1112 * is supported by looking at the MMD registers 3.20 and 7.60/61 1113 * and it programs the MMD register 3.0 setting the "Clock stop enable" 1114 * bit if required. 1115 */ 1116 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable) 1117 { 1118 if (!phydev->drv) 1119 return -EIO; 1120 1121 /* According to 802.3az,the EEE is supported only in full duplex-mode. 1122 */ 1123 if (phydev->duplex == DUPLEX_FULL) { 1124 int eee_lp, eee_cap, eee_adv; 1125 u32 lp, cap, adv; 1126 int status; 1127 1128 /* Read phy status to properly get the right settings */ 1129 status = phy_read_status(phydev); 1130 if (status) 1131 return status; 1132 1133 /* First check if the EEE ability is supported */ 1134 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 1135 if (eee_cap <= 0) 1136 goto eee_exit_err; 1137 1138 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap); 1139 if (!cap) 1140 goto eee_exit_err; 1141 1142 /* Check which link settings negotiated and verify it in 1143 * the EEE advertising registers. 1144 */ 1145 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); 1146 if (eee_lp <= 0) 1147 goto eee_exit_err; 1148 1149 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 1150 if (eee_adv <= 0) 1151 goto eee_exit_err; 1152 1153 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv); 1154 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp); 1155 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv)) 1156 goto eee_exit_err; 1157 1158 if (clk_stop_enable) { 1159 /* Configure the PHY to stop receiving xMII 1160 * clock while it is signaling LPI. 1161 */ 1162 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); 1163 if (val < 0) 1164 return val; 1165 1166 val |= MDIO_PCS_CTRL1_CLKSTOP_EN; 1167 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val); 1168 } 1169 1170 return 0; /* EEE supported */ 1171 } 1172 eee_exit_err: 1173 return -EPROTONOSUPPORT; 1174 } 1175 EXPORT_SYMBOL(phy_init_eee); 1176 1177 /** 1178 * phy_get_eee_err - report the EEE wake error count 1179 * @phydev: target phy_device struct 1180 * 1181 * Description: it is to report the number of time where the PHY 1182 * failed to complete its normal wake sequence. 1183 */ 1184 int phy_get_eee_err(struct phy_device *phydev) 1185 { 1186 if (!phydev->drv) 1187 return -EIO; 1188 1189 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR); 1190 } 1191 EXPORT_SYMBOL(phy_get_eee_err); 1192 1193 /** 1194 * phy_ethtool_get_eee - get EEE supported and status 1195 * @phydev: target phy_device struct 1196 * @data: ethtool_eee data 1197 * 1198 * Description: it reportes the Supported/Advertisement/LP Advertisement 1199 * capabilities. 1200 */ 1201 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data) 1202 { 1203 int val; 1204 1205 if (!phydev->drv) 1206 return -EIO; 1207 1208 /* Get Supported EEE */ 1209 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 1210 if (val < 0) 1211 return val; 1212 data->supported = mmd_eee_cap_to_ethtool_sup_t(val); 1213 1214 /* Get advertisement EEE */ 1215 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 1216 if (val < 0) 1217 return val; 1218 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val); 1219 1220 /* Get LP advertisement EEE */ 1221 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); 1222 if (val < 0) 1223 return val; 1224 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val); 1225 1226 return 0; 1227 } 1228 EXPORT_SYMBOL(phy_ethtool_get_eee); 1229 1230 /** 1231 * phy_ethtool_set_eee - set EEE supported and status 1232 * @phydev: target phy_device struct 1233 * @data: ethtool_eee data 1234 * 1235 * Description: it is to program the Advertisement EEE register. 1236 */ 1237 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data) 1238 { 1239 int cap, old_adv, adv, ret; 1240 1241 if (!phydev->drv) 1242 return -EIO; 1243 1244 /* Get Supported EEE */ 1245 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 1246 if (cap < 0) 1247 return cap; 1248 1249 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 1250 if (old_adv < 0) 1251 return old_adv; 1252 1253 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap; 1254 1255 /* Mask prohibited EEE modes */ 1256 adv &= ~phydev->eee_broken_modes; 1257 1258 if (old_adv != adv) { 1259 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv); 1260 if (ret < 0) 1261 return ret; 1262 1263 /* Restart autonegotiation so the new modes get sent to the 1264 * link partner. 1265 */ 1266 ret = phy_restart_aneg(phydev); 1267 if (ret < 0) 1268 return ret; 1269 } 1270 1271 return 0; 1272 } 1273 EXPORT_SYMBOL(phy_ethtool_set_eee); 1274 1275 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) 1276 { 1277 if (phydev->drv && phydev->drv->set_wol) 1278 return phydev->drv->set_wol(phydev, wol); 1279 1280 return -EOPNOTSUPP; 1281 } 1282 EXPORT_SYMBOL(phy_ethtool_set_wol); 1283 1284 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) 1285 { 1286 if (phydev->drv && phydev->drv->get_wol) 1287 phydev->drv->get_wol(phydev, wol); 1288 } 1289 EXPORT_SYMBOL(phy_ethtool_get_wol); 1290 1291 int phy_ethtool_get_link_ksettings(struct net_device *ndev, 1292 struct ethtool_link_ksettings *cmd) 1293 { 1294 struct phy_device *phydev = ndev->phydev; 1295 1296 if (!phydev) 1297 return -ENODEV; 1298 1299 phy_ethtool_ksettings_get(phydev, cmd); 1300 1301 return 0; 1302 } 1303 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings); 1304 1305 int phy_ethtool_set_link_ksettings(struct net_device *ndev, 1306 const struct ethtool_link_ksettings *cmd) 1307 { 1308 struct phy_device *phydev = ndev->phydev; 1309 1310 if (!phydev) 1311 return -ENODEV; 1312 1313 return phy_ethtool_ksettings_set(phydev, cmd); 1314 } 1315 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings); 1316 1317 int phy_ethtool_nway_reset(struct net_device *ndev) 1318 { 1319 struct phy_device *phydev = ndev->phydev; 1320 1321 if (!phydev) 1322 return -ENODEV; 1323 1324 if (!phydev->drv) 1325 return -EIO; 1326 1327 return phy_restart_aneg(phydev); 1328 } 1329 EXPORT_SYMBOL(phy_ethtool_nway_reset); 1330