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