| 1 | // SPDX-License-Identifier: GPL-2.0+ | 
|---|
| 2 | /* Framework for configuring and reading PHY devices | 
|---|
| 3 | * Based on code in sungem_phy.c and gianfar_phy.c | 
|---|
| 4 | * | 
|---|
| 5 | * Author: Andy Fleming | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (c) 2004 Freescale Semiconductor, Inc. | 
|---|
| 8 | * Copyright (c) 2006, 2007  Maciej W. Rozycki | 
|---|
| 9 | */ | 
|---|
| 10 |  | 
|---|
| 11 | #include <linux/kernel.h> | 
|---|
| 12 | #include <linux/string.h> | 
|---|
| 13 | #include <linux/errno.h> | 
|---|
| 14 | #include <linux/unistd.h> | 
|---|
| 15 | #include <linux/interrupt.h> | 
|---|
| 16 | #include <linux/delay.h> | 
|---|
| 17 | #include <linux/netdevice.h> | 
|---|
| 18 | #include <linux/netlink.h> | 
|---|
| 19 | #include <linux/etherdevice.h> | 
|---|
| 20 | #include <linux/skbuff.h> | 
|---|
| 21 | #include <linux/mm.h> | 
|---|
| 22 | #include <linux/module.h> | 
|---|
| 23 | #include <linux/mii.h> | 
|---|
| 24 | #include <linux/ethtool.h> | 
|---|
| 25 | #include <linux/ethtool_netlink.h> | 
|---|
| 26 | #include <linux/phy.h> | 
|---|
| 27 | #include <linux/phy_led_triggers.h> | 
|---|
| 28 | #include <linux/sfp.h> | 
|---|
| 29 | #include <linux/workqueue.h> | 
|---|
| 30 | #include <linux/mdio.h> | 
|---|
| 31 | #include <linux/io.h> | 
|---|
| 32 | #include <linux/uaccess.h> | 
|---|
| 33 | #include <linux/atomic.h> | 
|---|
| 34 | #include <linux/suspend.h> | 
|---|
| 35 | #include <net/netlink.h> | 
|---|
| 36 | #include <net/genetlink.h> | 
|---|
| 37 | #include <net/sock.h> | 
|---|
| 38 |  | 
|---|
| 39 | #include "phylib-internal.h" | 
|---|
| 40 | #include "phy-caps.h" | 
|---|
| 41 |  | 
|---|
| 42 | #define PHY_STATE_TIME	HZ | 
|---|
| 43 |  | 
|---|
| 44 | #define PHY_STATE_STR(_state)			\ | 
|---|
| 45 | case PHY_##_state:			\ | 
|---|
| 46 | return __stringify(_state);	\ | 
|---|
| 47 |  | 
|---|
| 48 | static const char *phy_state_to_str(enum phy_state st) | 
|---|
| 49 | { | 
|---|
| 50 | switch (st) { | 
|---|
| 51 | PHY_STATE_STR(DOWN) | 
|---|
| 52 | PHY_STATE_STR(READY) | 
|---|
| 53 | PHY_STATE_STR(UP) | 
|---|
| 54 | PHY_STATE_STR(RUNNING) | 
|---|
| 55 | PHY_STATE_STR(NOLINK) | 
|---|
| 56 | PHY_STATE_STR(CABLETEST) | 
|---|
| 57 | PHY_STATE_STR(HALTED) | 
|---|
| 58 | PHY_STATE_STR(ERROR) | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | return NULL; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | static void phy_process_state_change(struct phy_device *phydev, | 
|---|
| 65 | enum phy_state old_state) | 
|---|
| 66 | { | 
|---|
| 67 | if (old_state != phydev->state) { | 
|---|
| 68 | phydev_dbg(phydev, "PHY state change %s -> %s\n", | 
|---|
| 69 | phy_state_to_str(old_state), | 
|---|
| 70 | phy_state_to_str(phydev->state)); | 
|---|
| 71 | if (phydev->drv && phydev->drv->link_change_notify) | 
|---|
| 72 | phydev->drv->link_change_notify(phydev); | 
|---|
| 73 | } | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | static void phy_link_up(struct phy_device *phydev) | 
|---|
| 77 | { | 
|---|
| 78 | phydev->phy_link_change(phydev, true); | 
|---|
| 79 | phy_led_trigger_change_speed(phy: phydev); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | static void phy_link_down(struct phy_device *phydev) | 
|---|
| 83 | { | 
|---|
| 84 | phydev->phy_link_change(phydev, false); | 
|---|
| 85 | phy_led_trigger_change_speed(phy: phydev); | 
|---|
| 86 | WRITE_ONCE(phydev->link_down_events, phydev->link_down_events + 1); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | static const char *phy_pause_str(struct phy_device *phydev) | 
|---|
| 90 | { | 
|---|
| 91 | bool local_pause, local_asym_pause; | 
|---|
| 92 |  | 
|---|
| 93 | if (phydev->autoneg == AUTONEG_DISABLE) | 
|---|
| 94 | goto no_pause; | 
|---|
| 95 |  | 
|---|
| 96 | local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, | 
|---|
| 97 | phydev->advertising); | 
|---|
| 98 | local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, | 
|---|
| 99 | phydev->advertising); | 
|---|
| 100 |  | 
|---|
| 101 | if (local_pause && phydev->pause) | 
|---|
| 102 | return "rx/tx"; | 
|---|
| 103 |  | 
|---|
| 104 | if (local_asym_pause && phydev->asym_pause) { | 
|---|
| 105 | if (local_pause) | 
|---|
| 106 | return "rx"; | 
|---|
| 107 | if (phydev->pause) | 
|---|
| 108 | return "tx"; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | no_pause: | 
|---|
| 112 | return "off"; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | /** | 
|---|
| 116 | * phy_print_status - Convenience function to print out the current phy status | 
|---|
| 117 | * @phydev: the phy_device struct | 
|---|
| 118 | */ | 
|---|
| 119 | void phy_print_status(struct phy_device *phydev) | 
|---|
| 120 | { | 
|---|
| 121 | if (phydev->link) { | 
|---|
| 122 | netdev_info(dev: phydev->attached_dev, | 
|---|
| 123 | format: "Link is Up - %s/%s %s- flow control %s\n", | 
|---|
| 124 | phy_speed_to_str(speed: phydev->speed), | 
|---|
| 125 | phy_duplex_to_str(duplex: phydev->duplex), | 
|---|
| 126 | phydev->downshifted_rate ? "(downshifted) ": "", | 
|---|
| 127 | phy_pause_str(phydev)); | 
|---|
| 128 | } else	{ | 
|---|
| 129 | netdev_info(dev: phydev->attached_dev, format: "Link is Down\n"); | 
|---|
| 130 | } | 
|---|
| 131 | } | 
|---|
| 132 | EXPORT_SYMBOL(phy_print_status); | 
|---|
| 133 |  | 
|---|
| 134 | /** | 
|---|
| 135 | * phy_get_rate_matching - determine if rate matching is supported | 
|---|
| 136 | * @phydev: The phy device to return rate matching for | 
|---|
| 137 | * @iface: The interface mode to use | 
|---|
| 138 | * | 
|---|
| 139 | * This determines the type of rate matching (if any) that @phy supports | 
|---|
| 140 | * using @iface. @iface may be %PHY_INTERFACE_MODE_NA to determine if any | 
|---|
| 141 | * interface supports rate matching. | 
|---|
| 142 | * | 
|---|
| 143 | * Return: The type of rate matching @phy supports for @iface, or | 
|---|
| 144 | *         %RATE_MATCH_NONE. | 
|---|
| 145 | */ | 
|---|
| 146 | int phy_get_rate_matching(struct phy_device *phydev, | 
|---|
| 147 | phy_interface_t iface) | 
|---|
| 148 | { | 
|---|
| 149 | int ret = RATE_MATCH_NONE; | 
|---|
| 150 |  | 
|---|
| 151 | if (phydev->drv->get_rate_matching) { | 
|---|
| 152 | mutex_lock(lock: &phydev->lock); | 
|---|
| 153 | ret = phydev->drv->get_rate_matching(phydev, iface); | 
|---|
| 154 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | return ret; | 
|---|
| 158 | } | 
|---|
| 159 | EXPORT_SYMBOL_GPL(phy_get_rate_matching); | 
|---|
| 160 |  | 
|---|
| 161 | /** | 
|---|
| 162 | * phy_config_interrupt - configure the PHY device for the requested interrupts | 
|---|
| 163 | * @phydev: the phy_device struct | 
|---|
| 164 | * @interrupts: interrupt flags to configure for this @phydev | 
|---|
| 165 | * | 
|---|
| 166 | * Returns 0 on success or < 0 on error. | 
|---|
| 167 | */ | 
|---|
| 168 | static int phy_config_interrupt(struct phy_device *phydev, bool interrupts) | 
|---|
| 169 | { | 
|---|
| 170 | phydev->interrupts = interrupts ? 1 : 0; | 
|---|
| 171 | if (phydev->drv->config_intr) | 
|---|
| 172 | return phydev->drv->config_intr(phydev); | 
|---|
| 173 |  | 
|---|
| 174 | return 0; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | /** | 
|---|
| 178 | * phy_restart_aneg - restart auto-negotiation | 
|---|
| 179 | * @phydev: target phy_device struct | 
|---|
| 180 | * | 
|---|
| 181 | * Restart the autonegotiation on @phydev.  Returns >= 0 on success or | 
|---|
| 182 | * negative errno on error. | 
|---|
| 183 | */ | 
|---|
| 184 | int phy_restart_aneg(struct phy_device *phydev) | 
|---|
| 185 | { | 
|---|
| 186 | int ret; | 
|---|
| 187 |  | 
|---|
| 188 | if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0))) | 
|---|
| 189 | ret = genphy_c45_restart_aneg(phydev); | 
|---|
| 190 | else | 
|---|
| 191 | ret = genphy_restart_aneg(phydev); | 
|---|
| 192 |  | 
|---|
| 193 | return ret; | 
|---|
| 194 | } | 
|---|
| 195 | EXPORT_SYMBOL_GPL(phy_restart_aneg); | 
|---|
| 196 |  | 
|---|
| 197 | /** | 
|---|
| 198 | * phy_aneg_done - return auto-negotiation status | 
|---|
| 199 | * @phydev: target phy_device struct | 
|---|
| 200 | * | 
|---|
| 201 | * Description: Return the auto-negotiation status from this @phydev | 
|---|
| 202 | * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation | 
|---|
| 203 | * is still pending. | 
|---|
| 204 | */ | 
|---|
| 205 | int phy_aneg_done(struct phy_device *phydev) | 
|---|
| 206 | { | 
|---|
| 207 | if (phydev->drv && phydev->drv->aneg_done) | 
|---|
| 208 | return phydev->drv->aneg_done(phydev); | 
|---|
| 209 | else if (phydev->is_c45) | 
|---|
| 210 | return genphy_c45_aneg_done(phydev); | 
|---|
| 211 | else | 
|---|
| 212 | return genphy_aneg_done(phydev); | 
|---|
| 213 | } | 
|---|
| 214 | EXPORT_SYMBOL(phy_aneg_done); | 
|---|
| 215 |  | 
|---|
| 216 | /** | 
|---|
| 217 | * phy_supported_speeds - return all speeds currently supported by a phy device | 
|---|
| 218 | * @phy: The phy device to return supported speeds of. | 
|---|
| 219 | * @speeds: buffer to store supported speeds in. | 
|---|
| 220 | * @size:   size of speeds buffer. | 
|---|
| 221 | * | 
|---|
| 222 | * Description: Returns the number of supported speeds, and fills the speeds | 
|---|
| 223 | * buffer with the supported speeds. If speeds buffer is too small to contain | 
|---|
| 224 | * all currently supported speeds, will return as many speeds as can fit. | 
|---|
| 225 | */ | 
|---|
| 226 | unsigned int phy_supported_speeds(struct phy_device *phy, | 
|---|
| 227 | unsigned int *speeds, | 
|---|
| 228 | unsigned int size) | 
|---|
| 229 | { | 
|---|
| 230 | return phy_caps_speeds(speeds, size, linkmodes: phy->supported); | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | /** | 
|---|
| 234 | * phy_check_valid - check if there is a valid PHY setting which matches | 
|---|
| 235 | *		     speed, duplex, and feature mask | 
|---|
| 236 | * @speed: speed to match | 
|---|
| 237 | * @duplex: duplex to match | 
|---|
| 238 | * @features: A mask of the valid settings | 
|---|
| 239 | * | 
|---|
| 240 | * Description: Returns true if there is a valid setting, false otherwise. | 
|---|
| 241 | */ | 
|---|
| 242 | bool phy_check_valid(int speed, int duplex, unsigned long *features) | 
|---|
| 243 | { | 
|---|
| 244 | return phy_caps_valid(speed, duplex, linkmodes: features); | 
|---|
| 245 | } | 
|---|
| 246 | EXPORT_SYMBOL(phy_check_valid); | 
|---|
| 247 |  | 
|---|
| 248 | /** | 
|---|
| 249 | * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex | 
|---|
| 250 | * @phydev: the target phy_device struct | 
|---|
| 251 | * | 
|---|
| 252 | * Description: Make sure the PHY is set to supported speeds and | 
|---|
| 253 | *   duplexes.  Drop down by one in this order:  1000/FULL, | 
|---|
| 254 | *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. | 
|---|
| 255 | */ | 
|---|
| 256 | static void phy_sanitize_settings(struct phy_device *phydev) | 
|---|
| 257 | { | 
|---|
| 258 | const struct link_capabilities *c; | 
|---|
| 259 |  | 
|---|
| 260 | c = phy_caps_lookup(speed: phydev->speed, duplex: phydev->duplex, supported: phydev->supported, | 
|---|
| 261 | exact: false); | 
|---|
| 262 |  | 
|---|
| 263 | if (c) { | 
|---|
| 264 | phydev->speed = c->speed; | 
|---|
| 265 | phydev->duplex = c->duplex; | 
|---|
| 266 | } else { | 
|---|
| 267 | /* We failed to find anything (no supported speeds?) */ | 
|---|
| 268 | phydev->speed = SPEED_UNKNOWN; | 
|---|
| 269 | phydev->duplex = DUPLEX_UNKNOWN; | 
|---|
| 270 | } | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | void phy_ethtool_ksettings_get(struct phy_device *phydev, | 
|---|
| 274 | struct ethtool_link_ksettings *cmd) | 
|---|
| 275 | { | 
|---|
| 276 | mutex_lock(lock: &phydev->lock); | 
|---|
| 277 | linkmode_copy(dst: cmd->link_modes.supported, src: phydev->supported); | 
|---|
| 278 | linkmode_copy(dst: cmd->link_modes.advertising, src: phydev->advertising); | 
|---|
| 279 | linkmode_copy(dst: cmd->link_modes.lp_advertising, src: phydev->lp_advertising); | 
|---|
| 280 |  | 
|---|
| 281 | cmd->base.speed = phydev->speed; | 
|---|
| 282 | cmd->base.duplex = phydev->duplex; | 
|---|
| 283 | cmd->base.master_slave_cfg = phydev->master_slave_get; | 
|---|
| 284 | cmd->base.master_slave_state = phydev->master_slave_state; | 
|---|
| 285 | cmd->base.rate_matching = phydev->rate_matching; | 
|---|
| 286 | if (phydev->interface == PHY_INTERFACE_MODE_MOCA) | 
|---|
| 287 | cmd->base.port = PORT_BNC; | 
|---|
| 288 | else | 
|---|
| 289 | cmd->base.port = phydev->port; | 
|---|
| 290 | cmd->base.transceiver = phydev->is_internal ? | 
|---|
| 291 | XCVR_INTERNAL : XCVR_EXTERNAL; | 
|---|
| 292 | cmd->base.phy_address = phydev->mdio.addr; | 
|---|
| 293 | cmd->base.autoneg = phydev->autoneg; | 
|---|
| 294 | cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl; | 
|---|
| 295 | cmd->base.eth_tp_mdix = phydev->mdix; | 
|---|
| 296 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 297 | } | 
|---|
| 298 | EXPORT_SYMBOL(phy_ethtool_ksettings_get); | 
|---|
| 299 |  | 
|---|
| 300 | /** | 
|---|
| 301 | * phy_mii_ioctl - generic PHY MII ioctl interface | 
|---|
| 302 | * @phydev: the phy_device struct | 
|---|
| 303 | * @ifr: &struct ifreq for socket ioctl's | 
|---|
| 304 | * @cmd: ioctl cmd to execute | 
|---|
| 305 | * | 
|---|
| 306 | * Note that this function is currently incompatible with the | 
|---|
| 307 | * PHYCONTROL layer.  It changes registers without regard to | 
|---|
| 308 | * current state.  Use at own risk. | 
|---|
| 309 | */ | 
|---|
| 310 | int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd) | 
|---|
| 311 | { | 
|---|
| 312 | struct mii_ioctl_data *mii_data = if_mii(rq: ifr); | 
|---|
| 313 | struct kernel_hwtstamp_config kernel_cfg; | 
|---|
| 314 | struct netlink_ext_ack extack = {}; | 
|---|
| 315 | u16 val = mii_data->val_in; | 
|---|
| 316 | bool change_autoneg = false; | 
|---|
| 317 | struct hwtstamp_config cfg; | 
|---|
| 318 | int prtad, devad; | 
|---|
| 319 | int ret; | 
|---|
| 320 |  | 
|---|
| 321 | switch (cmd) { | 
|---|
| 322 | case SIOCGMIIPHY: | 
|---|
| 323 | mii_data->phy_id = phydev->mdio.addr; | 
|---|
| 324 | fallthrough; | 
|---|
| 325 |  | 
|---|
| 326 | case SIOCGMIIREG: | 
|---|
| 327 | if (mdio_phy_id_is_c45(phy_id: mii_data->phy_id)) { | 
|---|
| 328 | prtad = mdio_phy_id_prtad(phy_id: mii_data->phy_id); | 
|---|
| 329 | devad = mdio_phy_id_devad(phy_id: mii_data->phy_id); | 
|---|
| 330 | ret = mdiobus_c45_read(bus: phydev->mdio.bus, addr: prtad, devad, | 
|---|
| 331 | regnum: mii_data->reg_num); | 
|---|
| 332 |  | 
|---|
| 333 | } else { | 
|---|
| 334 | ret = mdiobus_read(bus: phydev->mdio.bus, addr: mii_data->phy_id, | 
|---|
| 335 | regnum: mii_data->reg_num); | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | if (ret < 0) | 
|---|
| 339 | return ret; | 
|---|
| 340 |  | 
|---|
| 341 | mii_data->val_out = ret; | 
|---|
| 342 |  | 
|---|
| 343 | return 0; | 
|---|
| 344 |  | 
|---|
| 345 | case SIOCSMIIREG: | 
|---|
| 346 | if (mdio_phy_id_is_c45(phy_id: mii_data->phy_id)) { | 
|---|
| 347 | prtad = mdio_phy_id_prtad(phy_id: mii_data->phy_id); | 
|---|
| 348 | devad = mdio_phy_id_devad(phy_id: mii_data->phy_id); | 
|---|
| 349 | } else { | 
|---|
| 350 | prtad = mii_data->phy_id; | 
|---|
| 351 | devad = mii_data->reg_num; | 
|---|
| 352 | } | 
|---|
| 353 | if (prtad == phydev->mdio.addr) { | 
|---|
| 354 | switch (devad) { | 
|---|
| 355 | case MII_BMCR: | 
|---|
| 356 | if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) { | 
|---|
| 357 | if (phydev->autoneg == AUTONEG_ENABLE) | 
|---|
| 358 | change_autoneg = true; | 
|---|
| 359 | phydev->autoneg = AUTONEG_DISABLE; | 
|---|
| 360 | if (val & BMCR_FULLDPLX) | 
|---|
| 361 | phydev->duplex = DUPLEX_FULL; | 
|---|
| 362 | else | 
|---|
| 363 | phydev->duplex = DUPLEX_HALF; | 
|---|
| 364 | if (val & BMCR_SPEED1000) | 
|---|
| 365 | phydev->speed = SPEED_1000; | 
|---|
| 366 | else if (val & BMCR_SPEED100) | 
|---|
| 367 | phydev->speed = SPEED_100; | 
|---|
| 368 | else phydev->speed = SPEED_10; | 
|---|
| 369 | } else { | 
|---|
| 370 | if (phydev->autoneg == AUTONEG_DISABLE) | 
|---|
| 371 | change_autoneg = true; | 
|---|
| 372 | phydev->autoneg = AUTONEG_ENABLE; | 
|---|
| 373 | } | 
|---|
| 374 | break; | 
|---|
| 375 | case MII_ADVERTISE: | 
|---|
| 376 | mii_adv_mod_linkmode_adv_t(advertising: phydev->advertising, | 
|---|
| 377 | adv: val); | 
|---|
| 378 | change_autoneg = true; | 
|---|
| 379 | break; | 
|---|
| 380 | case MII_CTRL1000: | 
|---|
| 381 | mii_ctrl1000_mod_linkmode_adv_t(advertising: phydev->advertising, | 
|---|
| 382 | ctrl1000: val); | 
|---|
| 383 | change_autoneg = true; | 
|---|
| 384 | break; | 
|---|
| 385 | default: | 
|---|
| 386 | /* do nothing */ | 
|---|
| 387 | break; | 
|---|
| 388 | } | 
|---|
| 389 | } | 
|---|
| 390 |  | 
|---|
| 391 | if (mdio_phy_id_is_c45(phy_id: mii_data->phy_id)) | 
|---|
| 392 | mdiobus_c45_write(bus: phydev->mdio.bus, addr: prtad, devad, | 
|---|
| 393 | regnum: mii_data->reg_num, val); | 
|---|
| 394 | else | 
|---|
| 395 | mdiobus_write(bus: phydev->mdio.bus, addr: prtad, regnum: devad, val); | 
|---|
| 396 |  | 
|---|
| 397 | if (prtad == phydev->mdio.addr && | 
|---|
| 398 | devad == MII_BMCR && | 
|---|
| 399 | val & BMCR_RESET) | 
|---|
| 400 | return phy_init_hw(phydev); | 
|---|
| 401 |  | 
|---|
| 402 | if (change_autoneg) | 
|---|
| 403 | return phy_start_aneg(phydev); | 
|---|
| 404 |  | 
|---|
| 405 | return 0; | 
|---|
| 406 |  | 
|---|
| 407 | case SIOCSHWTSTAMP: | 
|---|
| 408 | if (phydev->mii_ts && phydev->mii_ts->hwtstamp) { | 
|---|
| 409 | if (copy_from_user(to: &cfg, from: ifr->ifr_data, n: sizeof(cfg))) | 
|---|
| 410 | return -EFAULT; | 
|---|
| 411 |  | 
|---|
| 412 | hwtstamp_config_to_kernel(kernel_cfg: &kernel_cfg, cfg: &cfg); | 
|---|
| 413 | ret = phydev->mii_ts->hwtstamp(phydev->mii_ts, &kernel_cfg, &extack); | 
|---|
| 414 | if (ret) | 
|---|
| 415 | return ret; | 
|---|
| 416 |  | 
|---|
| 417 | hwtstamp_config_from_kernel(cfg: &cfg, kernel_cfg: &kernel_cfg); | 
|---|
| 418 | if (copy_to_user(to: ifr->ifr_data, from: &cfg, n: sizeof(cfg))) | 
|---|
| 419 | return -EFAULT; | 
|---|
| 420 |  | 
|---|
| 421 | return 0; | 
|---|
| 422 | } | 
|---|
| 423 | fallthrough; | 
|---|
| 424 |  | 
|---|
| 425 | default: | 
|---|
| 426 | return -EOPNOTSUPP; | 
|---|
| 427 | } | 
|---|
| 428 | } | 
|---|
| 429 | EXPORT_SYMBOL(phy_mii_ioctl); | 
|---|
| 430 |  | 
|---|
| 431 | /** | 
|---|
| 432 | * phy_do_ioctl - generic ndo_eth_ioctl implementation | 
|---|
| 433 | * @dev: the net_device struct | 
|---|
| 434 | * @ifr: &struct ifreq for socket ioctl's | 
|---|
| 435 | * @cmd: ioctl cmd to execute | 
|---|
| 436 | */ | 
|---|
| 437 | int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) | 
|---|
| 438 | { | 
|---|
| 439 | if (!dev->phydev) | 
|---|
| 440 | return -ENODEV; | 
|---|
| 441 |  | 
|---|
| 442 | return phy_mii_ioctl(dev->phydev, ifr, cmd); | 
|---|
| 443 | } | 
|---|
| 444 | EXPORT_SYMBOL(phy_do_ioctl); | 
|---|
| 445 |  | 
|---|
| 446 | /** | 
|---|
| 447 | * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first | 
|---|
| 448 | * | 
|---|
| 449 | * @dev: the net_device struct | 
|---|
| 450 | * @ifr: &struct ifreq for socket ioctl's | 
|---|
| 451 | * @cmd: ioctl cmd to execute | 
|---|
| 452 | * | 
|---|
| 453 | * Same as phy_do_ioctl, but ensures that net_device is running before | 
|---|
| 454 | * handling the ioctl. | 
|---|
| 455 | */ | 
|---|
| 456 | int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd) | 
|---|
| 457 | { | 
|---|
| 458 | if (!netif_running(dev)) | 
|---|
| 459 | return -ENODEV; | 
|---|
| 460 |  | 
|---|
| 461 | return phy_do_ioctl(dev, ifr, cmd); | 
|---|
| 462 | } | 
|---|
| 463 | EXPORT_SYMBOL(phy_do_ioctl_running); | 
|---|
| 464 |  | 
|---|
| 465 | /** | 
|---|
| 466 | * __phy_hwtstamp_get - Get hardware timestamping configuration from PHY | 
|---|
| 467 | * | 
|---|
| 468 | * @phydev: the PHY device structure | 
|---|
| 469 | * @config: structure holding the timestamping configuration | 
|---|
| 470 | * | 
|---|
| 471 | * Query the PHY device for its current hardware timestamping configuration. | 
|---|
| 472 | */ | 
|---|
| 473 | int __phy_hwtstamp_get(struct phy_device *phydev, | 
|---|
| 474 | struct kernel_hwtstamp_config *config) | 
|---|
| 475 | { | 
|---|
| 476 | if (!phydev) | 
|---|
| 477 | return -ENODEV; | 
|---|
| 478 |  | 
|---|
| 479 | return -EOPNOTSUPP; | 
|---|
| 480 | } | 
|---|
| 481 |  | 
|---|
| 482 | /** | 
|---|
| 483 | * __phy_hwtstamp_set - Modify PHY hardware timestamping configuration | 
|---|
| 484 | * | 
|---|
| 485 | * @phydev: the PHY device structure | 
|---|
| 486 | * @config: structure holding the timestamping configuration | 
|---|
| 487 | * @extack: netlink extended ack structure, for error reporting | 
|---|
| 488 | */ | 
|---|
| 489 | int __phy_hwtstamp_set(struct phy_device *phydev, | 
|---|
| 490 | struct kernel_hwtstamp_config *config, | 
|---|
| 491 | struct netlink_ext_ack *extack) | 
|---|
| 492 | { | 
|---|
| 493 | if (!phydev) | 
|---|
| 494 | return -ENODEV; | 
|---|
| 495 |  | 
|---|
| 496 | if (phydev->mii_ts && phydev->mii_ts->hwtstamp) | 
|---|
| 497 | return phydev->mii_ts->hwtstamp(phydev->mii_ts, config, extack); | 
|---|
| 498 |  | 
|---|
| 499 | return -EOPNOTSUPP; | 
|---|
| 500 | } | 
|---|
| 501 |  | 
|---|
| 502 | /** | 
|---|
| 503 | * phy_queue_state_machine - Trigger the state machine to run soon | 
|---|
| 504 | * | 
|---|
| 505 | * @phydev: the phy_device struct | 
|---|
| 506 | * @jiffies: Run the state machine after these jiffies | 
|---|
| 507 | */ | 
|---|
| 508 | static void phy_queue_state_machine(struct phy_device *phydev, | 
|---|
| 509 | unsigned long jiffies) | 
|---|
| 510 | { | 
|---|
| 511 | mod_delayed_work(wq: system_power_efficient_wq, dwork: &phydev->state_queue, | 
|---|
| 512 | delay: jiffies); | 
|---|
| 513 | } | 
|---|
| 514 |  | 
|---|
| 515 | /** | 
|---|
| 516 | * phy_trigger_machine - Trigger the state machine to run now | 
|---|
| 517 | * | 
|---|
| 518 | * @phydev: the phy_device struct | 
|---|
| 519 | */ | 
|---|
| 520 | void phy_trigger_machine(struct phy_device *phydev) | 
|---|
| 521 | { | 
|---|
| 522 | phy_queue_state_machine(phydev, jiffies: 0); | 
|---|
| 523 | } | 
|---|
| 524 | EXPORT_SYMBOL(phy_trigger_machine); | 
|---|
| 525 |  | 
|---|
| 526 | static void phy_abort_cable_test(struct phy_device *phydev) | 
|---|
| 527 | { | 
|---|
| 528 | int err; | 
|---|
| 529 |  | 
|---|
| 530 | ethnl_cable_test_finished(phydev); | 
|---|
| 531 |  | 
|---|
| 532 | err = phy_init_hw(phydev); | 
|---|
| 533 | if (err) | 
|---|
| 534 | phydev_err(phydev, "Error while aborting cable test"); | 
|---|
| 535 | } | 
|---|
| 536 |  | 
|---|
| 537 | /** | 
|---|
| 538 | * phy_ethtool_get_strings - Get the statistic counter names | 
|---|
| 539 | * | 
|---|
| 540 | * @phydev: the phy_device struct | 
|---|
| 541 | * @data: Where to put the strings | 
|---|
| 542 | */ | 
|---|
| 543 | int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data) | 
|---|
| 544 | { | 
|---|
| 545 | if (!phydev->drv) | 
|---|
| 546 | return -EIO; | 
|---|
| 547 |  | 
|---|
| 548 | mutex_lock(lock: &phydev->lock); | 
|---|
| 549 | phydev->drv->get_strings(phydev, data); | 
|---|
| 550 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 551 |  | 
|---|
| 552 | return 0; | 
|---|
| 553 | } | 
|---|
| 554 | EXPORT_SYMBOL(phy_ethtool_get_strings); | 
|---|
| 555 |  | 
|---|
| 556 | /** | 
|---|
| 557 | * phy_ethtool_get_sset_count - Get the number of statistic counters | 
|---|
| 558 | * | 
|---|
| 559 | * @phydev: the phy_device struct | 
|---|
| 560 | */ | 
|---|
| 561 | int phy_ethtool_get_sset_count(struct phy_device *phydev) | 
|---|
| 562 | { | 
|---|
| 563 | int ret; | 
|---|
| 564 |  | 
|---|
| 565 | if (!phydev->drv) | 
|---|
| 566 | return -EIO; | 
|---|
| 567 |  | 
|---|
| 568 | if (phydev->drv->get_sset_count && | 
|---|
| 569 | phydev->drv->get_strings && | 
|---|
| 570 | phydev->drv->get_stats) { | 
|---|
| 571 | mutex_lock(lock: &phydev->lock); | 
|---|
| 572 | ret = phydev->drv->get_sset_count(phydev); | 
|---|
| 573 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 574 |  | 
|---|
| 575 | return ret; | 
|---|
| 576 | } | 
|---|
| 577 |  | 
|---|
| 578 | return -EOPNOTSUPP; | 
|---|
| 579 | } | 
|---|
| 580 | EXPORT_SYMBOL(phy_ethtool_get_sset_count); | 
|---|
| 581 |  | 
|---|
| 582 | /** | 
|---|
| 583 | * phy_ethtool_get_stats - Get the statistic counters | 
|---|
| 584 | * | 
|---|
| 585 | * @phydev: the phy_device struct | 
|---|
| 586 | * @stats: What counters to get | 
|---|
| 587 | * @data: Where to store the counters | 
|---|
| 588 | */ | 
|---|
| 589 | int phy_ethtool_get_stats(struct phy_device *phydev, | 
|---|
| 590 | struct ethtool_stats *stats, u64 *data) | 
|---|
| 591 | { | 
|---|
| 592 | if (!phydev->drv) | 
|---|
| 593 | return -EIO; | 
|---|
| 594 |  | 
|---|
| 595 | mutex_lock(lock: &phydev->lock); | 
|---|
| 596 | phydev->drv->get_stats(phydev, stats, data); | 
|---|
| 597 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 598 |  | 
|---|
| 599 | return 0; | 
|---|
| 600 | } | 
|---|
| 601 | EXPORT_SYMBOL(phy_ethtool_get_stats); | 
|---|
| 602 |  | 
|---|
| 603 | /** | 
|---|
| 604 | * __phy_ethtool_get_phy_stats - Retrieve standardized PHY statistics | 
|---|
| 605 | * @phydev: Pointer to the PHY device | 
|---|
| 606 | * @phy_stats: Pointer to ethtool_eth_phy_stats structure | 
|---|
| 607 | * @phydev_stats: Pointer to ethtool_phy_stats structure | 
|---|
| 608 | * | 
|---|
| 609 | * Fetches PHY statistics using a kernel-defined interface for consistent | 
|---|
| 610 | * diagnostics. Unlike phy_ethtool_get_stats(), which allows custom stats, | 
|---|
| 611 | * this function enforces a standardized format for better interoperability. | 
|---|
| 612 | */ | 
|---|
| 613 | void __phy_ethtool_get_phy_stats(struct phy_device *phydev, | 
|---|
| 614 | struct ethtool_eth_phy_stats *phy_stats, | 
|---|
| 615 | struct ethtool_phy_stats *phydev_stats) | 
|---|
| 616 | { | 
|---|
| 617 | if (!phydev->drv || !phydev->drv->get_phy_stats) | 
|---|
| 618 | return; | 
|---|
| 619 |  | 
|---|
| 620 | mutex_lock(lock: &phydev->lock); | 
|---|
| 621 | phydev->drv->get_phy_stats(phydev, phy_stats, phydev_stats); | 
|---|
| 622 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 623 | } | 
|---|
| 624 |  | 
|---|
| 625 | /** | 
|---|
| 626 | * __phy_ethtool_get_link_ext_stats - Retrieve extended link statistics for a PHY | 
|---|
| 627 | * @phydev: Pointer to the PHY device | 
|---|
| 628 | * @link_stats: Pointer to the structure to store extended link statistics | 
|---|
| 629 | * | 
|---|
| 630 | * Populates the ethtool_link_ext_stats structure with link down event counts | 
|---|
| 631 | * and additional driver-specific link statistics, if available. | 
|---|
| 632 | */ | 
|---|
| 633 | void __phy_ethtool_get_link_ext_stats(struct phy_device *phydev, | 
|---|
| 634 | struct ethtool_link_ext_stats *link_stats) | 
|---|
| 635 | { | 
|---|
| 636 | link_stats->link_down_events = READ_ONCE(phydev->link_down_events); | 
|---|
| 637 |  | 
|---|
| 638 | if (!phydev->drv || !phydev->drv->get_link_stats) | 
|---|
| 639 | return; | 
|---|
| 640 |  | 
|---|
| 641 | mutex_lock(lock: &phydev->lock); | 
|---|
| 642 | phydev->drv->get_link_stats(phydev, link_stats); | 
|---|
| 643 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 644 | } | 
|---|
| 645 |  | 
|---|
| 646 | /** | 
|---|
| 647 | * phy_ethtool_get_plca_cfg - Get PLCA RS configuration | 
|---|
| 648 | * @phydev: the phy_device struct | 
|---|
| 649 | * @plca_cfg: where to store the retrieved configuration | 
|---|
| 650 | * | 
|---|
| 651 | * Retrieve the PLCA configuration from the PHY. Return 0 on success or a | 
|---|
| 652 | * negative value if an error occurred. | 
|---|
| 653 | */ | 
|---|
| 654 | int phy_ethtool_get_plca_cfg(struct phy_device *phydev, | 
|---|
| 655 | struct phy_plca_cfg *plca_cfg) | 
|---|
| 656 | { | 
|---|
| 657 | int ret; | 
|---|
| 658 |  | 
|---|
| 659 | if (!phydev->drv) { | 
|---|
| 660 | ret = -EIO; | 
|---|
| 661 | goto out; | 
|---|
| 662 | } | 
|---|
| 663 |  | 
|---|
| 664 | if (!phydev->drv->get_plca_cfg) { | 
|---|
| 665 | ret = -EOPNOTSUPP; | 
|---|
| 666 | goto out; | 
|---|
| 667 | } | 
|---|
| 668 |  | 
|---|
| 669 | mutex_lock(lock: &phydev->lock); | 
|---|
| 670 | ret = phydev->drv->get_plca_cfg(phydev, plca_cfg); | 
|---|
| 671 |  | 
|---|
| 672 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 673 | out: | 
|---|
| 674 | return ret; | 
|---|
| 675 | } | 
|---|
| 676 |  | 
|---|
| 677 | /** | 
|---|
| 678 | * plca_check_valid - Check PLCA configuration before enabling | 
|---|
| 679 | * @phydev: the phy_device struct | 
|---|
| 680 | * @plca_cfg: current PLCA configuration | 
|---|
| 681 | * @extack: extack for reporting useful error messages | 
|---|
| 682 | * | 
|---|
| 683 | * Checks whether the PLCA and PHY configuration are consistent and it is safe | 
|---|
| 684 | * to enable PLCA. Returns 0 on success or a negative value if the PLCA or PHY | 
|---|
| 685 | * configuration is not consistent. | 
|---|
| 686 | */ | 
|---|
| 687 | static int plca_check_valid(struct phy_device *phydev, | 
|---|
| 688 | const struct phy_plca_cfg *plca_cfg, | 
|---|
| 689 | struct netlink_ext_ack *extack) | 
|---|
| 690 | { | 
|---|
| 691 | int ret = 0; | 
|---|
| 692 |  | 
|---|
| 693 | if (!linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT, | 
|---|
| 694 | phydev->advertising)) { | 
|---|
| 695 | ret = -EOPNOTSUPP; | 
|---|
| 696 | NL_SET_ERR_MSG(extack, | 
|---|
| 697 | "Point to Multi-Point mode is not enabled"); | 
|---|
| 698 | } else if (plca_cfg->node_id >= 255) { | 
|---|
| 699 | NL_SET_ERR_MSG(extack, "PLCA node ID is not set"); | 
|---|
| 700 | ret = -EINVAL; | 
|---|
| 701 | } | 
|---|
| 702 |  | 
|---|
| 703 | return ret; | 
|---|
| 704 | } | 
|---|
| 705 |  | 
|---|
| 706 | /** | 
|---|
| 707 | * phy_ethtool_set_plca_cfg - Set PLCA RS configuration | 
|---|
| 708 | * @phydev: the phy_device struct | 
|---|
| 709 | * @plca_cfg: new PLCA configuration to apply | 
|---|
| 710 | * @extack: extack for reporting useful error messages | 
|---|
| 711 | * | 
|---|
| 712 | * Sets the PLCA configuration in the PHY. Return 0 on success or a | 
|---|
| 713 | * negative value if an error occurred. | 
|---|
| 714 | */ | 
|---|
| 715 | int phy_ethtool_set_plca_cfg(struct phy_device *phydev, | 
|---|
| 716 | const struct phy_plca_cfg *plca_cfg, | 
|---|
| 717 | struct netlink_ext_ack *extack) | 
|---|
| 718 | { | 
|---|
| 719 | struct phy_plca_cfg *curr_plca_cfg; | 
|---|
| 720 | int ret; | 
|---|
| 721 |  | 
|---|
| 722 | if (!phydev->drv) { | 
|---|
| 723 | ret = -EIO; | 
|---|
| 724 | goto out; | 
|---|
| 725 | } | 
|---|
| 726 |  | 
|---|
| 727 | if (!phydev->drv->set_plca_cfg || | 
|---|
| 728 | !phydev->drv->get_plca_cfg) { | 
|---|
| 729 | ret = -EOPNOTSUPP; | 
|---|
| 730 | goto out; | 
|---|
| 731 | } | 
|---|
| 732 |  | 
|---|
| 733 | curr_plca_cfg = kmalloc(sizeof(*curr_plca_cfg), GFP_KERNEL); | 
|---|
| 734 | if (!curr_plca_cfg) { | 
|---|
| 735 | ret = -ENOMEM; | 
|---|
| 736 | goto out; | 
|---|
| 737 | } | 
|---|
| 738 |  | 
|---|
| 739 | mutex_lock(lock: &phydev->lock); | 
|---|
| 740 |  | 
|---|
| 741 | ret = phydev->drv->get_plca_cfg(phydev, curr_plca_cfg); | 
|---|
| 742 | if (ret) | 
|---|
| 743 | goto out_drv; | 
|---|
| 744 |  | 
|---|
| 745 | if (curr_plca_cfg->enabled < 0 && plca_cfg->enabled >= 0) { | 
|---|
| 746 | NL_SET_ERR_MSG(extack, | 
|---|
| 747 | "PHY does not support changing the PLCA 'enable' attribute"); | 
|---|
| 748 | ret = -EINVAL; | 
|---|
| 749 | goto out_drv; | 
|---|
| 750 | } | 
|---|
| 751 |  | 
|---|
| 752 | if (curr_plca_cfg->node_id < 0 && plca_cfg->node_id >= 0) { | 
|---|
| 753 | NL_SET_ERR_MSG(extack, | 
|---|
| 754 | "PHY does not support changing the PLCA 'local node ID' attribute"); | 
|---|
| 755 | ret = -EINVAL; | 
|---|
| 756 | goto out_drv; | 
|---|
| 757 | } | 
|---|
| 758 |  | 
|---|
| 759 | if (curr_plca_cfg->node_cnt < 0 && plca_cfg->node_cnt >= 0) { | 
|---|
| 760 | NL_SET_ERR_MSG(extack, | 
|---|
| 761 | "PHY does not support changing the PLCA 'node count' attribute"); | 
|---|
| 762 | ret = -EINVAL; | 
|---|
| 763 | goto out_drv; | 
|---|
| 764 | } | 
|---|
| 765 |  | 
|---|
| 766 | if (curr_plca_cfg->to_tmr < 0 && plca_cfg->to_tmr >= 0) { | 
|---|
| 767 | NL_SET_ERR_MSG(extack, | 
|---|
| 768 | "PHY does not support changing the PLCA 'TO timer' attribute"); | 
|---|
| 769 | ret = -EINVAL; | 
|---|
| 770 | goto out_drv; | 
|---|
| 771 | } | 
|---|
| 772 |  | 
|---|
| 773 | if (curr_plca_cfg->burst_cnt < 0 && plca_cfg->burst_cnt >= 0) { | 
|---|
| 774 | NL_SET_ERR_MSG(extack, | 
|---|
| 775 | "PHY does not support changing the PLCA 'burst count' attribute"); | 
|---|
| 776 | ret = -EINVAL; | 
|---|
| 777 | goto out_drv; | 
|---|
| 778 | } | 
|---|
| 779 |  | 
|---|
| 780 | if (curr_plca_cfg->burst_tmr < 0 && plca_cfg->burst_tmr >= 0) { | 
|---|
| 781 | NL_SET_ERR_MSG(extack, | 
|---|
| 782 | "PHY does not support changing the PLCA 'burst timer' attribute"); | 
|---|
| 783 | ret = -EINVAL; | 
|---|
| 784 | goto out_drv; | 
|---|
| 785 | } | 
|---|
| 786 |  | 
|---|
| 787 | // if enabling PLCA, perform a few sanity checks | 
|---|
| 788 | if (plca_cfg->enabled > 0) { | 
|---|
| 789 | // allow setting node_id concurrently with enabled | 
|---|
| 790 | if (plca_cfg->node_id >= 0) | 
|---|
| 791 | curr_plca_cfg->node_id = plca_cfg->node_id; | 
|---|
| 792 |  | 
|---|
| 793 | ret = plca_check_valid(phydev, plca_cfg: curr_plca_cfg, extack); | 
|---|
| 794 | if (ret) | 
|---|
| 795 | goto out_drv; | 
|---|
| 796 | } | 
|---|
| 797 |  | 
|---|
| 798 | ret = phydev->drv->set_plca_cfg(phydev, plca_cfg); | 
|---|
| 799 |  | 
|---|
| 800 | out_drv: | 
|---|
| 801 | kfree(objp: curr_plca_cfg); | 
|---|
| 802 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 803 | out: | 
|---|
| 804 | return ret; | 
|---|
| 805 | } | 
|---|
| 806 |  | 
|---|
| 807 | /** | 
|---|
| 808 | * phy_ethtool_get_plca_status - Get PLCA RS status information | 
|---|
| 809 | * @phydev: the phy_device struct | 
|---|
| 810 | * @plca_st: where to store the retrieved status information | 
|---|
| 811 | * | 
|---|
| 812 | * Retrieve the PLCA status information from the PHY. Return 0 on success or a | 
|---|
| 813 | * negative value if an error occurred. | 
|---|
| 814 | */ | 
|---|
| 815 | int phy_ethtool_get_plca_status(struct phy_device *phydev, | 
|---|
| 816 | struct phy_plca_status *plca_st) | 
|---|
| 817 | { | 
|---|
| 818 | int ret; | 
|---|
| 819 |  | 
|---|
| 820 | if (!phydev->drv) { | 
|---|
| 821 | ret = -EIO; | 
|---|
| 822 | goto out; | 
|---|
| 823 | } | 
|---|
| 824 |  | 
|---|
| 825 | if (!phydev->drv->get_plca_status) { | 
|---|
| 826 | ret = -EOPNOTSUPP; | 
|---|
| 827 | goto out; | 
|---|
| 828 | } | 
|---|
| 829 |  | 
|---|
| 830 | mutex_lock(lock: &phydev->lock); | 
|---|
| 831 | ret = phydev->drv->get_plca_status(phydev, plca_st); | 
|---|
| 832 |  | 
|---|
| 833 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 834 | out: | 
|---|
| 835 | return ret; | 
|---|
| 836 | } | 
|---|
| 837 |  | 
|---|
| 838 | /** | 
|---|
| 839 | * phy_start_cable_test - Start a cable test | 
|---|
| 840 | * | 
|---|
| 841 | * @phydev: the phy_device struct | 
|---|
| 842 | * @extack: extack for reporting useful error messages | 
|---|
| 843 | */ | 
|---|
| 844 | int phy_start_cable_test(struct phy_device *phydev, | 
|---|
| 845 | struct netlink_ext_ack *extack) | 
|---|
| 846 | { | 
|---|
| 847 | struct net_device *dev = phydev->attached_dev; | 
|---|
| 848 | int err = -ENOMEM; | 
|---|
| 849 |  | 
|---|
| 850 | if (!(phydev->drv && | 
|---|
| 851 | phydev->drv->cable_test_start && | 
|---|
| 852 | phydev->drv->cable_test_get_status)) { | 
|---|
| 853 | NL_SET_ERR_MSG(extack, | 
|---|
| 854 | "PHY driver does not support cable testing"); | 
|---|
| 855 | return -EOPNOTSUPP; | 
|---|
| 856 | } | 
|---|
| 857 |  | 
|---|
| 858 | mutex_lock(lock: &phydev->lock); | 
|---|
| 859 | if (phydev->state == PHY_CABLETEST) { | 
|---|
| 860 | NL_SET_ERR_MSG(extack, | 
|---|
| 861 | "PHY already performing a test"); | 
|---|
| 862 | err = -EBUSY; | 
|---|
| 863 | goto out; | 
|---|
| 864 | } | 
|---|
| 865 |  | 
|---|
| 866 | if (phydev->state < PHY_UP || | 
|---|
| 867 | phydev->state > PHY_CABLETEST) { | 
|---|
| 868 | NL_SET_ERR_MSG(extack, | 
|---|
| 869 | "PHY not configured. Try setting interface up"); | 
|---|
| 870 | err = -EBUSY; | 
|---|
| 871 | goto out; | 
|---|
| 872 | } | 
|---|
| 873 |  | 
|---|
| 874 | err = ethnl_cable_test_alloc(phydev, cmd: ETHTOOL_MSG_CABLE_TEST_NTF); | 
|---|
| 875 | if (err) | 
|---|
| 876 | goto out; | 
|---|
| 877 |  | 
|---|
| 878 | /* Mark the carrier down until the test is complete */ | 
|---|
| 879 | phy_link_down(phydev); | 
|---|
| 880 |  | 
|---|
| 881 | netif_testing_on(dev); | 
|---|
| 882 | err = phydev->drv->cable_test_start(phydev); | 
|---|
| 883 | if (err) { | 
|---|
| 884 | netif_testing_off(dev); | 
|---|
| 885 | phy_link_up(phydev); | 
|---|
| 886 | goto out_free; | 
|---|
| 887 | } | 
|---|
| 888 |  | 
|---|
| 889 | phydev->state = PHY_CABLETEST; | 
|---|
| 890 |  | 
|---|
| 891 | if (phy_polling_mode(phydev)) | 
|---|
| 892 | phy_trigger_machine(phydev); | 
|---|
| 893 |  | 
|---|
| 894 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 895 |  | 
|---|
| 896 | return 0; | 
|---|
| 897 |  | 
|---|
| 898 | out_free: | 
|---|
| 899 | ethnl_cable_test_free(phydev); | 
|---|
| 900 | out: | 
|---|
| 901 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 902 |  | 
|---|
| 903 | return err; | 
|---|
| 904 | } | 
|---|
| 905 | EXPORT_SYMBOL(phy_start_cable_test); | 
|---|
| 906 |  | 
|---|
| 907 | /** | 
|---|
| 908 | * phy_start_cable_test_tdr - Start a raw TDR cable test | 
|---|
| 909 | * | 
|---|
| 910 | * @phydev: the phy_device struct | 
|---|
| 911 | * @extack: extack for reporting useful error messages | 
|---|
| 912 | * @config: Configuration of the test to run | 
|---|
| 913 | */ | 
|---|
| 914 | int phy_start_cable_test_tdr(struct phy_device *phydev, | 
|---|
| 915 | struct netlink_ext_ack *extack, | 
|---|
| 916 | const struct phy_tdr_config *config) | 
|---|
| 917 | { | 
|---|
| 918 | struct net_device *dev = phydev->attached_dev; | 
|---|
| 919 | int err = -ENOMEM; | 
|---|
| 920 |  | 
|---|
| 921 | if (!(phydev->drv && | 
|---|
| 922 | phydev->drv->cable_test_tdr_start && | 
|---|
| 923 | phydev->drv->cable_test_get_status)) { | 
|---|
| 924 | NL_SET_ERR_MSG(extack, | 
|---|
| 925 | "PHY driver does not support cable test TDR"); | 
|---|
| 926 | return -EOPNOTSUPP; | 
|---|
| 927 | } | 
|---|
| 928 |  | 
|---|
| 929 | mutex_lock(lock: &phydev->lock); | 
|---|
| 930 | if (phydev->state == PHY_CABLETEST) { | 
|---|
| 931 | NL_SET_ERR_MSG(extack, | 
|---|
| 932 | "PHY already performing a test"); | 
|---|
| 933 | err = -EBUSY; | 
|---|
| 934 | goto out; | 
|---|
| 935 | } | 
|---|
| 936 |  | 
|---|
| 937 | if (phydev->state < PHY_UP || | 
|---|
| 938 | phydev->state > PHY_CABLETEST) { | 
|---|
| 939 | NL_SET_ERR_MSG(extack, | 
|---|
| 940 | "PHY not configured. Try setting interface up"); | 
|---|
| 941 | err = -EBUSY; | 
|---|
| 942 | goto out; | 
|---|
| 943 | } | 
|---|
| 944 |  | 
|---|
| 945 | err = ethnl_cable_test_alloc(phydev, cmd: ETHTOOL_MSG_CABLE_TEST_TDR_NTF); | 
|---|
| 946 | if (err) | 
|---|
| 947 | goto out; | 
|---|
| 948 |  | 
|---|
| 949 | /* Mark the carrier down until the test is complete */ | 
|---|
| 950 | phy_link_down(phydev); | 
|---|
| 951 |  | 
|---|
| 952 | netif_testing_on(dev); | 
|---|
| 953 | err = phydev->drv->cable_test_tdr_start(phydev, config); | 
|---|
| 954 | if (err) { | 
|---|
| 955 | netif_testing_off(dev); | 
|---|
| 956 | phy_link_up(phydev); | 
|---|
| 957 | goto out_free; | 
|---|
| 958 | } | 
|---|
| 959 |  | 
|---|
| 960 | phydev->state = PHY_CABLETEST; | 
|---|
| 961 |  | 
|---|
| 962 | if (phy_polling_mode(phydev)) | 
|---|
| 963 | phy_trigger_machine(phydev); | 
|---|
| 964 |  | 
|---|
| 965 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 966 |  | 
|---|
| 967 | return 0; | 
|---|
| 968 |  | 
|---|
| 969 | out_free: | 
|---|
| 970 | ethnl_cable_test_free(phydev); | 
|---|
| 971 | out: | 
|---|
| 972 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 973 |  | 
|---|
| 974 | return err; | 
|---|
| 975 | } | 
|---|
| 976 | EXPORT_SYMBOL(phy_start_cable_test_tdr); | 
|---|
| 977 |  | 
|---|
| 978 | int phy_config_aneg(struct phy_device *phydev) | 
|---|
| 979 | { | 
|---|
| 980 | if (phydev->drv->config_aneg) | 
|---|
| 981 | return phydev->drv->config_aneg(phydev); | 
|---|
| 982 |  | 
|---|
| 983 | /* Clause 45 PHYs that don't implement Clause 22 registers are not | 
|---|
| 984 | * allowed to call genphy_config_aneg() | 
|---|
| 985 | */ | 
|---|
| 986 | if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0))) | 
|---|
| 987 | return genphy_c45_config_aneg(phydev); | 
|---|
| 988 |  | 
|---|
| 989 | return genphy_config_aneg(phydev); | 
|---|
| 990 | } | 
|---|
| 991 | EXPORT_SYMBOL(phy_config_aneg); | 
|---|
| 992 |  | 
|---|
| 993 | /** | 
|---|
| 994 | * phy_check_link_status - check link status and set state accordingly | 
|---|
| 995 | * @phydev: the phy_device struct | 
|---|
| 996 | * | 
|---|
| 997 | * Description: Check for link and whether autoneg was triggered / is running | 
|---|
| 998 | * and set state accordingly | 
|---|
| 999 | */ | 
|---|
| 1000 | static int phy_check_link_status(struct phy_device *phydev) | 
|---|
| 1001 | { | 
|---|
| 1002 | int err; | 
|---|
| 1003 |  | 
|---|
| 1004 | lockdep_assert_held(&phydev->lock); | 
|---|
| 1005 |  | 
|---|
| 1006 | /* Keep previous state if loopback is enabled because some PHYs | 
|---|
| 1007 | * report that Link is Down when loopback is enabled. | 
|---|
| 1008 | */ | 
|---|
| 1009 | if (phydev->loopback_enabled) | 
|---|
| 1010 | return 0; | 
|---|
| 1011 |  | 
|---|
| 1012 | err = phy_read_status(phydev); | 
|---|
| 1013 | if (err) | 
|---|
| 1014 | return err; | 
|---|
| 1015 |  | 
|---|
| 1016 | if (phydev->link && phydev->state != PHY_RUNNING) { | 
|---|
| 1017 | phy_check_downshift(phydev); | 
|---|
| 1018 | phydev->state = PHY_RUNNING; | 
|---|
| 1019 | err = genphy_c45_eee_is_active(phydev, NULL); | 
|---|
| 1020 | phydev->eee_active = err > 0; | 
|---|
| 1021 | phydev->enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled && | 
|---|
| 1022 | phydev->eee_active; | 
|---|
| 1023 |  | 
|---|
| 1024 | phy_link_up(phydev); | 
|---|
| 1025 | } else if (!phydev->link && phydev->state != PHY_NOLINK) { | 
|---|
| 1026 | phydev->state = PHY_NOLINK; | 
|---|
| 1027 | phydev->eee_active = false; | 
|---|
| 1028 | phydev->enable_tx_lpi = false; | 
|---|
| 1029 | phy_link_down(phydev); | 
|---|
| 1030 | } | 
|---|
| 1031 |  | 
|---|
| 1032 | return 0; | 
|---|
| 1033 | } | 
|---|
| 1034 |  | 
|---|
| 1035 | /** | 
|---|
| 1036 | * phy_inband_caps - query which in-band signalling modes are supported | 
|---|
| 1037 | * @phydev: a pointer to a &struct phy_device | 
|---|
| 1038 | * @interface: the interface mode for the PHY | 
|---|
| 1039 | * | 
|---|
| 1040 | * Returns zero if it is unknown what in-band signalling is supported by the | 
|---|
| 1041 | * PHY (e.g. because the PHY driver doesn't implement the method.) Otherwise, | 
|---|
| 1042 | * returns a bit mask of the LINK_INBAND_* values from | 
|---|
| 1043 | * &enum link_inband_signalling to describe which inband modes are supported | 
|---|
| 1044 | * by the PHY for this interface mode. | 
|---|
| 1045 | */ | 
|---|
| 1046 | unsigned int phy_inband_caps(struct phy_device *phydev, | 
|---|
| 1047 | phy_interface_t interface) | 
|---|
| 1048 | { | 
|---|
| 1049 | if (phydev->drv && phydev->drv->inband_caps) | 
|---|
| 1050 | return phydev->drv->inband_caps(phydev, interface); | 
|---|
| 1051 |  | 
|---|
| 1052 | return 0; | 
|---|
| 1053 | } | 
|---|
| 1054 | EXPORT_SYMBOL_GPL(phy_inband_caps); | 
|---|
| 1055 |  | 
|---|
| 1056 | /** | 
|---|
| 1057 | * phy_config_inband - configure the desired PHY in-band mode | 
|---|
| 1058 | * @phydev: the phy_device struct | 
|---|
| 1059 | * @modes: in-band modes to configure | 
|---|
| 1060 | * | 
|---|
| 1061 | * Description: disables, enables or enables-with-bypass in-band signalling | 
|---|
| 1062 | *   between the PHY and host system. | 
|---|
| 1063 | * | 
|---|
| 1064 | * Returns: zero on success, or negative errno value. | 
|---|
| 1065 | */ | 
|---|
| 1066 | int phy_config_inband(struct phy_device *phydev, unsigned int modes) | 
|---|
| 1067 | { | 
|---|
| 1068 | lockdep_assert_held(&phydev->lock); | 
|---|
| 1069 |  | 
|---|
| 1070 | if (!!(modes & LINK_INBAND_DISABLE) + | 
|---|
| 1071 | !!(modes & LINK_INBAND_ENABLE) + | 
|---|
| 1072 | !!(modes & LINK_INBAND_BYPASS) != 1) | 
|---|
| 1073 | return -EINVAL; | 
|---|
| 1074 |  | 
|---|
| 1075 | if (!phydev->drv) | 
|---|
| 1076 | return -EIO; | 
|---|
| 1077 | else if (!phydev->drv->config_inband) | 
|---|
| 1078 | return -EOPNOTSUPP; | 
|---|
| 1079 |  | 
|---|
| 1080 | return phydev->drv->config_inband(phydev, modes); | 
|---|
| 1081 | } | 
|---|
| 1082 | EXPORT_SYMBOL(phy_config_inband); | 
|---|
| 1083 |  | 
|---|
| 1084 | /** | 
|---|
| 1085 | * _phy_start_aneg - start auto-negotiation for this PHY device | 
|---|
| 1086 | * @phydev: the phy_device struct | 
|---|
| 1087 | * | 
|---|
| 1088 | * Description: Sanitizes the settings (if we're not autonegotiating | 
|---|
| 1089 | *   them), and then calls the driver's config_aneg function. | 
|---|
| 1090 | *   If the PHYCONTROL Layer is operating, we change the state to | 
|---|
| 1091 | *   reflect the beginning of Auto-negotiation or forcing. | 
|---|
| 1092 | */ | 
|---|
| 1093 | int _phy_start_aneg(struct phy_device *phydev) | 
|---|
| 1094 | { | 
|---|
| 1095 | int err; | 
|---|
| 1096 |  | 
|---|
| 1097 | lockdep_assert_held(&phydev->lock); | 
|---|
| 1098 |  | 
|---|
| 1099 | if (!phydev->drv) | 
|---|
| 1100 | return -EIO; | 
|---|
| 1101 |  | 
|---|
| 1102 | if (AUTONEG_DISABLE == phydev->autoneg) | 
|---|
| 1103 | phy_sanitize_settings(phydev); | 
|---|
| 1104 |  | 
|---|
| 1105 | err = phy_config_aneg(phydev); | 
|---|
| 1106 | if (err < 0) | 
|---|
| 1107 | return err; | 
|---|
| 1108 |  | 
|---|
| 1109 | if (phy_is_started(phydev)) | 
|---|
| 1110 | err = phy_check_link_status(phydev); | 
|---|
| 1111 |  | 
|---|
| 1112 | return err; | 
|---|
| 1113 | } | 
|---|
| 1114 | EXPORT_SYMBOL(_phy_start_aneg); | 
|---|
| 1115 |  | 
|---|
| 1116 | /** | 
|---|
| 1117 | * phy_start_aneg - start auto-negotiation for this PHY device | 
|---|
| 1118 | * @phydev: the phy_device struct | 
|---|
| 1119 | * | 
|---|
| 1120 | * Description: Sanitizes the settings (if we're not autonegotiating | 
|---|
| 1121 | *   them), and then calls the driver's config_aneg function. | 
|---|
| 1122 | *   If the PHYCONTROL Layer is operating, we change the state to | 
|---|
| 1123 | *   reflect the beginning of Auto-negotiation or forcing. | 
|---|
| 1124 | */ | 
|---|
| 1125 | int phy_start_aneg(struct phy_device *phydev) | 
|---|
| 1126 | { | 
|---|
| 1127 | int err; | 
|---|
| 1128 |  | 
|---|
| 1129 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1130 | err = _phy_start_aneg(phydev); | 
|---|
| 1131 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1132 |  | 
|---|
| 1133 | return err; | 
|---|
| 1134 | } | 
|---|
| 1135 | EXPORT_SYMBOL(phy_start_aneg); | 
|---|
| 1136 |  | 
|---|
| 1137 | static int phy_poll_aneg_done(struct phy_device *phydev) | 
|---|
| 1138 | { | 
|---|
| 1139 | unsigned int retries = 100; | 
|---|
| 1140 | int ret; | 
|---|
| 1141 |  | 
|---|
| 1142 | do { | 
|---|
| 1143 | msleep(msecs: 100); | 
|---|
| 1144 | ret = phy_aneg_done(phydev); | 
|---|
| 1145 | } while (!ret && --retries); | 
|---|
| 1146 |  | 
|---|
| 1147 | if (!ret) | 
|---|
| 1148 | return -ETIMEDOUT; | 
|---|
| 1149 |  | 
|---|
| 1150 | return ret < 0 ? ret : 0; | 
|---|
| 1151 | } | 
|---|
| 1152 |  | 
|---|
| 1153 | int phy_ethtool_ksettings_set(struct phy_device *phydev, | 
|---|
| 1154 | const struct ethtool_link_ksettings *cmd) | 
|---|
| 1155 | { | 
|---|
| 1156 | __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); | 
|---|
| 1157 | u8 autoneg = cmd->base.autoneg; | 
|---|
| 1158 | u8 duplex = cmd->base.duplex; | 
|---|
| 1159 | u32 speed = cmd->base.speed; | 
|---|
| 1160 |  | 
|---|
| 1161 | if (cmd->base.phy_address != phydev->mdio.addr) | 
|---|
| 1162 | return -EINVAL; | 
|---|
| 1163 |  | 
|---|
| 1164 | linkmode_copy(dst: advertising, src: cmd->link_modes.advertising); | 
|---|
| 1165 |  | 
|---|
| 1166 | /* We make sure that we don't pass unsupported values in to the PHY */ | 
|---|
| 1167 | linkmode_and(dst: advertising, a: advertising, b: phydev->supported); | 
|---|
| 1168 |  | 
|---|
| 1169 | /* Verify the settings we care about. */ | 
|---|
| 1170 | if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE) | 
|---|
| 1171 | return -EINVAL; | 
|---|
| 1172 |  | 
|---|
| 1173 | if (autoneg == AUTONEG_ENABLE && | 
|---|
| 1174 | (linkmode_empty(src: advertising) || | 
|---|
| 1175 | !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, | 
|---|
| 1176 | phydev->supported))) | 
|---|
| 1177 | return -EINVAL; | 
|---|
| 1178 |  | 
|---|
| 1179 | if (autoneg == AUTONEG_DISABLE && | 
|---|
| 1180 | ((speed != SPEED_1000 && | 
|---|
| 1181 | speed != SPEED_100 && | 
|---|
| 1182 | speed != SPEED_10) || | 
|---|
| 1183 | (duplex != DUPLEX_HALF && | 
|---|
| 1184 | duplex != DUPLEX_FULL))) | 
|---|
| 1185 | return -EINVAL; | 
|---|
| 1186 |  | 
|---|
| 1187 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1188 | phydev->autoneg = autoneg; | 
|---|
| 1189 |  | 
|---|
| 1190 | if (autoneg == AUTONEG_DISABLE) { | 
|---|
| 1191 | phydev->speed = speed; | 
|---|
| 1192 | phydev->duplex = duplex; | 
|---|
| 1193 | } | 
|---|
| 1194 |  | 
|---|
| 1195 | linkmode_copy(dst: phydev->advertising, src: advertising); | 
|---|
| 1196 |  | 
|---|
| 1197 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, | 
|---|
| 1198 | phydev->advertising, autoneg == AUTONEG_ENABLE); | 
|---|
| 1199 |  | 
|---|
| 1200 | phydev->master_slave_set = cmd->base.master_slave_cfg; | 
|---|
| 1201 | phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl; | 
|---|
| 1202 |  | 
|---|
| 1203 | /* Restart the PHY */ | 
|---|
| 1204 | if (phy_is_started(phydev)) { | 
|---|
| 1205 | phydev->state = PHY_UP; | 
|---|
| 1206 | phy_trigger_machine(phydev); | 
|---|
| 1207 | } else { | 
|---|
| 1208 | _phy_start_aneg(phydev); | 
|---|
| 1209 | } | 
|---|
| 1210 |  | 
|---|
| 1211 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1212 | return 0; | 
|---|
| 1213 | } | 
|---|
| 1214 | EXPORT_SYMBOL(phy_ethtool_ksettings_set); | 
|---|
| 1215 |  | 
|---|
| 1216 | /** | 
|---|
| 1217 | * phy_speed_down - set speed to lowest speed supported by both link partners | 
|---|
| 1218 | * @phydev: the phy_device struct | 
|---|
| 1219 | * @sync: perform action synchronously | 
|---|
| 1220 | * | 
|---|
| 1221 | * Description: Typically used to save energy when waiting for a WoL packet | 
|---|
| 1222 | * | 
|---|
| 1223 | * WARNING: Setting sync to false may cause the system being unable to suspend | 
|---|
| 1224 | * in case the PHY generates an interrupt when finishing the autonegotiation. | 
|---|
| 1225 | * This interrupt may wake up the system immediately after suspend. | 
|---|
| 1226 | * Therefore use sync = false only if you're sure it's safe with the respective | 
|---|
| 1227 | * network chip. | 
|---|
| 1228 | */ | 
|---|
| 1229 | int phy_speed_down(struct phy_device *phydev, bool sync) | 
|---|
| 1230 | { | 
|---|
| 1231 | __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp); | 
|---|
| 1232 | int ret = 0; | 
|---|
| 1233 |  | 
|---|
| 1234 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1235 |  | 
|---|
| 1236 | if (phydev->autoneg != AUTONEG_ENABLE) | 
|---|
| 1237 | goto out; | 
|---|
| 1238 |  | 
|---|
| 1239 | linkmode_copy(dst: adv_tmp, src: phydev->advertising); | 
|---|
| 1240 |  | 
|---|
| 1241 | ret = phy_speed_down_core(phydev); | 
|---|
| 1242 | if (ret) | 
|---|
| 1243 | goto out; | 
|---|
| 1244 |  | 
|---|
| 1245 | linkmode_copy(dst: phydev->adv_old, src: adv_tmp); | 
|---|
| 1246 |  | 
|---|
| 1247 | if (linkmode_equal(src1: phydev->advertising, src2: adv_tmp)) { | 
|---|
| 1248 | ret = 0; | 
|---|
| 1249 | goto out; | 
|---|
| 1250 | } | 
|---|
| 1251 |  | 
|---|
| 1252 | ret = phy_config_aneg(phydev); | 
|---|
| 1253 | if (ret) | 
|---|
| 1254 | goto out; | 
|---|
| 1255 |  | 
|---|
| 1256 | ret = sync ? phy_poll_aneg_done(phydev) : 0; | 
|---|
| 1257 | out: | 
|---|
| 1258 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1259 |  | 
|---|
| 1260 | return ret; | 
|---|
| 1261 | } | 
|---|
| 1262 | EXPORT_SYMBOL_GPL(phy_speed_down); | 
|---|
| 1263 |  | 
|---|
| 1264 | /** | 
|---|
| 1265 | * phy_speed_up - (re)set advertised speeds to all supported speeds | 
|---|
| 1266 | * @phydev: the phy_device struct | 
|---|
| 1267 | * | 
|---|
| 1268 | * Description: Used to revert the effect of phy_speed_down | 
|---|
| 1269 | */ | 
|---|
| 1270 | int phy_speed_up(struct phy_device *phydev) | 
|---|
| 1271 | { | 
|---|
| 1272 | __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp); | 
|---|
| 1273 | int ret = 0; | 
|---|
| 1274 |  | 
|---|
| 1275 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1276 |  | 
|---|
| 1277 | if (phydev->autoneg != AUTONEG_ENABLE) | 
|---|
| 1278 | goto out; | 
|---|
| 1279 |  | 
|---|
| 1280 | if (linkmode_empty(src: phydev->adv_old)) | 
|---|
| 1281 | goto out; | 
|---|
| 1282 |  | 
|---|
| 1283 | linkmode_copy(dst: adv_tmp, src: phydev->advertising); | 
|---|
| 1284 | linkmode_copy(dst: phydev->advertising, src: phydev->adv_old); | 
|---|
| 1285 | linkmode_zero(dst: phydev->adv_old); | 
|---|
| 1286 |  | 
|---|
| 1287 | if (linkmode_equal(src1: phydev->advertising, src2: adv_tmp)) | 
|---|
| 1288 | goto out; | 
|---|
| 1289 |  | 
|---|
| 1290 | ret = phy_config_aneg(phydev); | 
|---|
| 1291 | out: | 
|---|
| 1292 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1293 |  | 
|---|
| 1294 | return ret; | 
|---|
| 1295 | } | 
|---|
| 1296 | EXPORT_SYMBOL_GPL(phy_speed_up); | 
|---|
| 1297 |  | 
|---|
| 1298 | /** | 
|---|
| 1299 | * phy_start_machine - start PHY state machine tracking | 
|---|
| 1300 | * @phydev: the phy_device struct | 
|---|
| 1301 | * | 
|---|
| 1302 | * Description: The PHY infrastructure can run a state machine | 
|---|
| 1303 | *   which tracks whether the PHY is starting up, negotiating, | 
|---|
| 1304 | *   etc.  This function starts the delayed workqueue which tracks | 
|---|
| 1305 | *   the state of the PHY. If you want to maintain your own state machine, | 
|---|
| 1306 | *   do not call this function. | 
|---|
| 1307 | */ | 
|---|
| 1308 | void phy_start_machine(struct phy_device *phydev) | 
|---|
| 1309 | { | 
|---|
| 1310 | phy_trigger_machine(phydev); | 
|---|
| 1311 | } | 
|---|
| 1312 | EXPORT_SYMBOL_GPL(phy_start_machine); | 
|---|
| 1313 |  | 
|---|
| 1314 | /** | 
|---|
| 1315 | * phy_stop_machine - stop the PHY state machine tracking | 
|---|
| 1316 | * @phydev: target phy_device struct | 
|---|
| 1317 | * | 
|---|
| 1318 | * Description: Stops the state machine delayed workqueue, sets the | 
|---|
| 1319 | *   state to UP (unless it wasn't up yet). This function must be | 
|---|
| 1320 | *   called BEFORE phy_detach. | 
|---|
| 1321 | */ | 
|---|
| 1322 | void phy_stop_machine(struct phy_device *phydev) | 
|---|
| 1323 | { | 
|---|
| 1324 | cancel_delayed_work_sync(dwork: &phydev->state_queue); | 
|---|
| 1325 |  | 
|---|
| 1326 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1327 | if (phy_is_started(phydev)) | 
|---|
| 1328 | phydev->state = PHY_UP; | 
|---|
| 1329 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1330 | } | 
|---|
| 1331 |  | 
|---|
| 1332 | static void phy_process_error(struct phy_device *phydev) | 
|---|
| 1333 | { | 
|---|
| 1334 | /* phydev->lock must be held for the state change to be safe */ | 
|---|
| 1335 | if (!mutex_is_locked(lock: &phydev->lock)) | 
|---|
| 1336 | phydev_err(phydev, "PHY-device data unsafe context\n"); | 
|---|
| 1337 |  | 
|---|
| 1338 | phydev->state = PHY_ERROR; | 
|---|
| 1339 |  | 
|---|
| 1340 | phy_trigger_machine(phydev); | 
|---|
| 1341 | } | 
|---|
| 1342 |  | 
|---|
| 1343 | static void phy_error_precise(struct phy_device *phydev, | 
|---|
| 1344 | const void *func, int err) | 
|---|
| 1345 | { | 
|---|
| 1346 | WARN(1, "%pS: returned: %d\n", func, err); | 
|---|
| 1347 | phy_process_error(phydev); | 
|---|
| 1348 | } | 
|---|
| 1349 |  | 
|---|
| 1350 | /** | 
|---|
| 1351 | * phy_error - enter ERROR state for this PHY device | 
|---|
| 1352 | * @phydev: target phy_device struct | 
|---|
| 1353 | * | 
|---|
| 1354 | * Moves the PHY to the ERROR state in response to a read | 
|---|
| 1355 | * or write error, and tells the controller the link is down. | 
|---|
| 1356 | * Must be called with phydev->lock held. | 
|---|
| 1357 | */ | 
|---|
| 1358 | void phy_error(struct phy_device *phydev) | 
|---|
| 1359 | { | 
|---|
| 1360 | WARN_ON(1); | 
|---|
| 1361 | phy_process_error(phydev); | 
|---|
| 1362 | } | 
|---|
| 1363 | EXPORT_SYMBOL(phy_error); | 
|---|
| 1364 |  | 
|---|
| 1365 | /** | 
|---|
| 1366 | * phy_disable_interrupts - Disable the PHY interrupts from the PHY side | 
|---|
| 1367 | * @phydev: target phy_device struct | 
|---|
| 1368 | */ | 
|---|
| 1369 | int phy_disable_interrupts(struct phy_device *phydev) | 
|---|
| 1370 | { | 
|---|
| 1371 | /* Disable PHY interrupts */ | 
|---|
| 1372 | return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); | 
|---|
| 1373 | } | 
|---|
| 1374 |  | 
|---|
| 1375 | /** | 
|---|
| 1376 | * phy_interrupt - PHY interrupt handler | 
|---|
| 1377 | * @irq: interrupt line | 
|---|
| 1378 | * @phy_dat: phy_device pointer | 
|---|
| 1379 | * | 
|---|
| 1380 | * Description: Handle PHY interrupt | 
|---|
| 1381 | */ | 
|---|
| 1382 | static irqreturn_t phy_interrupt(int irq, void *phy_dat) | 
|---|
| 1383 | { | 
|---|
| 1384 | struct phy_device *phydev = phy_dat; | 
|---|
| 1385 | irqreturn_t ret; | 
|---|
| 1386 |  | 
|---|
| 1387 | /* Wakeup interrupts may occur during a system sleep transition. | 
|---|
| 1388 | * Postpone handling until the PHY has resumed. | 
|---|
| 1389 | */ | 
|---|
| 1390 | if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) { | 
|---|
| 1391 | struct net_device *netdev = phydev->attached_dev; | 
|---|
| 1392 |  | 
|---|
| 1393 | if (netdev) { | 
|---|
| 1394 | struct device *parent = netdev->dev.parent; | 
|---|
| 1395 |  | 
|---|
| 1396 | if (netdev->ethtool->wol_enabled) | 
|---|
| 1397 | pm_system_wakeup(); | 
|---|
| 1398 | else if (device_may_wakeup(dev: &netdev->dev)) | 
|---|
| 1399 | pm_wakeup_dev_event(dev: &netdev->dev, msec: 0, hard: true); | 
|---|
| 1400 | else if (parent && device_may_wakeup(dev: parent)) | 
|---|
| 1401 | pm_wakeup_dev_event(dev: parent, msec: 0, hard: true); | 
|---|
| 1402 | } | 
|---|
| 1403 |  | 
|---|
| 1404 | phydev->irq_rerun = 1; | 
|---|
| 1405 | disable_irq_nosync(irq); | 
|---|
| 1406 | return IRQ_HANDLED; | 
|---|
| 1407 | } | 
|---|
| 1408 |  | 
|---|
| 1409 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1410 | ret = phydev->drv->handle_interrupt(phydev); | 
|---|
| 1411 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1412 |  | 
|---|
| 1413 | return ret; | 
|---|
| 1414 | } | 
|---|
| 1415 |  | 
|---|
| 1416 | /** | 
|---|
| 1417 | * phy_enable_interrupts - Enable the interrupts from the PHY side | 
|---|
| 1418 | * @phydev: target phy_device struct | 
|---|
| 1419 | */ | 
|---|
| 1420 | static int phy_enable_interrupts(struct phy_device *phydev) | 
|---|
| 1421 | { | 
|---|
| 1422 | return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); | 
|---|
| 1423 | } | 
|---|
| 1424 |  | 
|---|
| 1425 | /** | 
|---|
| 1426 | * phy_update_stats - Update PHY device statistics if supported. | 
|---|
| 1427 | * @phydev: Pointer to the PHY device structure. | 
|---|
| 1428 | * | 
|---|
| 1429 | * If the PHY driver provides an update_stats callback, this function | 
|---|
| 1430 | * invokes it to update the PHY statistics. If not, it returns 0. | 
|---|
| 1431 | * | 
|---|
| 1432 | * Return: 0 on success, or a negative error code if the callback fails. | 
|---|
| 1433 | */ | 
|---|
| 1434 | static int phy_update_stats(struct phy_device *phydev) | 
|---|
| 1435 | { | 
|---|
| 1436 | if (!phydev->drv->update_stats) | 
|---|
| 1437 | return 0; | 
|---|
| 1438 |  | 
|---|
| 1439 | return phydev->drv->update_stats(phydev); | 
|---|
| 1440 | } | 
|---|
| 1441 |  | 
|---|
| 1442 | /** | 
|---|
| 1443 | * phy_request_interrupt - request and enable interrupt for a PHY device | 
|---|
| 1444 | * @phydev: target phy_device struct | 
|---|
| 1445 | * | 
|---|
| 1446 | * Description: Request and enable the interrupt for the given PHY. | 
|---|
| 1447 | *   If this fails, then we set irq to PHY_POLL. | 
|---|
| 1448 | *   This should only be called with a valid IRQ number. | 
|---|
| 1449 | */ | 
|---|
| 1450 | void phy_request_interrupt(struct phy_device *phydev) | 
|---|
| 1451 | { | 
|---|
| 1452 | int err; | 
|---|
| 1453 |  | 
|---|
| 1454 | err = request_threaded_irq(irq: phydev->irq, NULL, thread_fn: phy_interrupt, | 
|---|
| 1455 | IRQF_ONESHOT | IRQF_SHARED, | 
|---|
| 1456 | name: phydev_name(phydev), dev: phydev); | 
|---|
| 1457 | if (err) { | 
|---|
| 1458 | phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n", | 
|---|
| 1459 | err, phydev->irq); | 
|---|
| 1460 | phydev->irq = PHY_POLL; | 
|---|
| 1461 | } else { | 
|---|
| 1462 | if (phy_enable_interrupts(phydev)) { | 
|---|
| 1463 | phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n"); | 
|---|
| 1464 | phy_free_interrupt(phydev); | 
|---|
| 1465 | phydev->irq = PHY_POLL; | 
|---|
| 1466 | } | 
|---|
| 1467 | } | 
|---|
| 1468 | } | 
|---|
| 1469 | EXPORT_SYMBOL(phy_request_interrupt); | 
|---|
| 1470 |  | 
|---|
| 1471 | /** | 
|---|
| 1472 | * phy_free_interrupt - disable and free interrupt for a PHY device | 
|---|
| 1473 | * @phydev: target phy_device struct | 
|---|
| 1474 | * | 
|---|
| 1475 | * Description: Disable and free the interrupt for the given PHY. | 
|---|
| 1476 | *   This should only be called with a valid IRQ number. | 
|---|
| 1477 | */ | 
|---|
| 1478 | void phy_free_interrupt(struct phy_device *phydev) | 
|---|
| 1479 | { | 
|---|
| 1480 | phy_disable_interrupts(phydev); | 
|---|
| 1481 | free_irq(phydev->irq, phydev); | 
|---|
| 1482 | } | 
|---|
| 1483 | EXPORT_SYMBOL(phy_free_interrupt); | 
|---|
| 1484 |  | 
|---|
| 1485 | /** | 
|---|
| 1486 | * phy_get_next_update_time - Determine the next PHY update time | 
|---|
| 1487 | * @phydev: Pointer to the phy_device structure | 
|---|
| 1488 | * | 
|---|
| 1489 | * This function queries the PHY driver to get the time for the next polling | 
|---|
| 1490 | * event. If the driver does not implement the callback, a default value is | 
|---|
| 1491 | * used. | 
|---|
| 1492 | * | 
|---|
| 1493 | * Return: The time for the next polling event in jiffies | 
|---|
| 1494 | */ | 
|---|
| 1495 | static unsigned int phy_get_next_update_time(struct phy_device *phydev) | 
|---|
| 1496 | { | 
|---|
| 1497 | if (phydev->drv && phydev->drv->get_next_update_time) | 
|---|
| 1498 | return phydev->drv->get_next_update_time(phydev); | 
|---|
| 1499 |  | 
|---|
| 1500 | return PHY_STATE_TIME; | 
|---|
| 1501 | } | 
|---|
| 1502 |  | 
|---|
| 1503 | enum phy_state_work { | 
|---|
| 1504 | PHY_STATE_WORK_NONE, | 
|---|
| 1505 | PHY_STATE_WORK_ANEG, | 
|---|
| 1506 | PHY_STATE_WORK_SUSPEND, | 
|---|
| 1507 | }; | 
|---|
| 1508 |  | 
|---|
| 1509 | static enum phy_state_work _phy_state_machine(struct phy_device *phydev) | 
|---|
| 1510 | { | 
|---|
| 1511 | enum phy_state_work state_work = PHY_STATE_WORK_NONE; | 
|---|
| 1512 | struct net_device *dev = phydev->attached_dev; | 
|---|
| 1513 | enum phy_state old_state = phydev->state; | 
|---|
| 1514 | const void *func = NULL; | 
|---|
| 1515 | bool finished = false; | 
|---|
| 1516 | int err = 0; | 
|---|
| 1517 |  | 
|---|
| 1518 | switch (phydev->state) { | 
|---|
| 1519 | case PHY_DOWN: | 
|---|
| 1520 | case PHY_READY: | 
|---|
| 1521 | break; | 
|---|
| 1522 | case PHY_UP: | 
|---|
| 1523 | state_work = PHY_STATE_WORK_ANEG; | 
|---|
| 1524 | break; | 
|---|
| 1525 | case PHY_NOLINK: | 
|---|
| 1526 | case PHY_RUNNING: | 
|---|
| 1527 | err = phy_check_link_status(phydev); | 
|---|
| 1528 | func = &phy_check_link_status; | 
|---|
| 1529 |  | 
|---|
| 1530 | if (!err) | 
|---|
| 1531 | err = phy_update_stats(phydev); | 
|---|
| 1532 | break; | 
|---|
| 1533 | case PHY_CABLETEST: | 
|---|
| 1534 | err = phydev->drv->cable_test_get_status(phydev, &finished); | 
|---|
| 1535 | if (err) { | 
|---|
| 1536 | phy_abort_cable_test(phydev); | 
|---|
| 1537 | netif_testing_off(dev); | 
|---|
| 1538 | state_work = PHY_STATE_WORK_ANEG; | 
|---|
| 1539 | phydev->state = PHY_UP; | 
|---|
| 1540 | break; | 
|---|
| 1541 | } | 
|---|
| 1542 |  | 
|---|
| 1543 | if (finished) { | 
|---|
| 1544 | ethnl_cable_test_finished(phydev); | 
|---|
| 1545 | netif_testing_off(dev); | 
|---|
| 1546 | state_work = PHY_STATE_WORK_ANEG; | 
|---|
| 1547 | phydev->state = PHY_UP; | 
|---|
| 1548 | } | 
|---|
| 1549 | break; | 
|---|
| 1550 | case PHY_HALTED: | 
|---|
| 1551 | if (phydev->link) { | 
|---|
| 1552 | if (phydev->autoneg == AUTONEG_ENABLE) { | 
|---|
| 1553 | phydev->speed = SPEED_UNKNOWN; | 
|---|
| 1554 | phydev->duplex = DUPLEX_UNKNOWN; | 
|---|
| 1555 | } | 
|---|
| 1556 | if (phydev->master_slave_state != | 
|---|
| 1557 | MASTER_SLAVE_STATE_UNSUPPORTED) | 
|---|
| 1558 | phydev->master_slave_state = | 
|---|
| 1559 | MASTER_SLAVE_STATE_UNKNOWN; | 
|---|
| 1560 | phydev->mdix = ETH_TP_MDI_INVALID; | 
|---|
| 1561 | linkmode_zero(dst: phydev->lp_advertising); | 
|---|
| 1562 | } | 
|---|
| 1563 | fallthrough; | 
|---|
| 1564 | case PHY_ERROR: | 
|---|
| 1565 | if (phydev->link) { | 
|---|
| 1566 | phydev->link = 0; | 
|---|
| 1567 | phydev->eee_active = false; | 
|---|
| 1568 | phydev->enable_tx_lpi = false; | 
|---|
| 1569 | phy_link_down(phydev); | 
|---|
| 1570 | } | 
|---|
| 1571 | state_work = PHY_STATE_WORK_SUSPEND; | 
|---|
| 1572 | break; | 
|---|
| 1573 | } | 
|---|
| 1574 |  | 
|---|
| 1575 | if (state_work == PHY_STATE_WORK_ANEG) { | 
|---|
| 1576 | err = _phy_start_aneg(phydev); | 
|---|
| 1577 | func = &_phy_start_aneg; | 
|---|
| 1578 | } | 
|---|
| 1579 |  | 
|---|
| 1580 | if (err == -ENODEV) | 
|---|
| 1581 | return state_work; | 
|---|
| 1582 |  | 
|---|
| 1583 | if (err < 0) | 
|---|
| 1584 | phy_error_precise(phydev, func, err); | 
|---|
| 1585 |  | 
|---|
| 1586 | phy_process_state_change(phydev, old_state); | 
|---|
| 1587 |  | 
|---|
| 1588 | /* Only re-schedule a PHY state machine change if we are polling the | 
|---|
| 1589 | * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving | 
|---|
| 1590 | * between states from phy_mac_interrupt(). | 
|---|
| 1591 | * | 
|---|
| 1592 | * In state PHY_HALTED the PHY gets suspended, so rescheduling the | 
|---|
| 1593 | * state machine would be pointless and possibly error prone when | 
|---|
| 1594 | * called from phy_disconnect() synchronously. | 
|---|
| 1595 | */ | 
|---|
| 1596 | if (phy_polling_mode(phydev) && phy_is_started(phydev)) | 
|---|
| 1597 | phy_queue_state_machine(phydev, | 
|---|
| 1598 | jiffies: phy_get_next_update_time(phydev)); | 
|---|
| 1599 |  | 
|---|
| 1600 | return state_work; | 
|---|
| 1601 | } | 
|---|
| 1602 |  | 
|---|
| 1603 | /* unlocked part of the PHY state machine */ | 
|---|
| 1604 | static void _phy_state_machine_post_work(struct phy_device *phydev, | 
|---|
| 1605 | enum phy_state_work state_work) | 
|---|
| 1606 | { | 
|---|
| 1607 | if (state_work == PHY_STATE_WORK_SUSPEND) | 
|---|
| 1608 | phy_suspend(phydev); | 
|---|
| 1609 | } | 
|---|
| 1610 |  | 
|---|
| 1611 | /** | 
|---|
| 1612 | * phy_state_machine - Handle the state machine | 
|---|
| 1613 | * @work: work_struct that describes the work to be done | 
|---|
| 1614 | */ | 
|---|
| 1615 | void phy_state_machine(struct work_struct *work) | 
|---|
| 1616 | { | 
|---|
| 1617 | struct delayed_work *dwork = to_delayed_work(work); | 
|---|
| 1618 | struct phy_device *phydev = | 
|---|
| 1619 | container_of(dwork, struct phy_device, state_queue); | 
|---|
| 1620 | enum phy_state_work state_work; | 
|---|
| 1621 |  | 
|---|
| 1622 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1623 | state_work = _phy_state_machine(phydev); | 
|---|
| 1624 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1625 |  | 
|---|
| 1626 | _phy_state_machine_post_work(phydev, state_work); | 
|---|
| 1627 | } | 
|---|
| 1628 |  | 
|---|
| 1629 | /** | 
|---|
| 1630 | * phy_stop - Bring down the PHY link, and stop checking the status | 
|---|
| 1631 | * @phydev: target phy_device struct | 
|---|
| 1632 | */ | 
|---|
| 1633 | void phy_stop(struct phy_device *phydev) | 
|---|
| 1634 | { | 
|---|
| 1635 | struct net_device *dev = phydev->attached_dev; | 
|---|
| 1636 | enum phy_state_work state_work; | 
|---|
| 1637 | enum phy_state old_state; | 
|---|
| 1638 |  | 
|---|
| 1639 | if (!phy_is_started(phydev) && phydev->state != PHY_DOWN && | 
|---|
| 1640 | phydev->state != PHY_ERROR) { | 
|---|
| 1641 | WARN(1, "called from state %s\n", | 
|---|
| 1642 | phy_state_to_str(phydev->state)); | 
|---|
| 1643 | return; | 
|---|
| 1644 | } | 
|---|
| 1645 |  | 
|---|
| 1646 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1647 | old_state = phydev->state; | 
|---|
| 1648 |  | 
|---|
| 1649 | if (phydev->state == PHY_CABLETEST) { | 
|---|
| 1650 | phy_abort_cable_test(phydev); | 
|---|
| 1651 | netif_testing_off(dev); | 
|---|
| 1652 | } | 
|---|
| 1653 |  | 
|---|
| 1654 | if (phydev->sfp_bus) | 
|---|
| 1655 | sfp_upstream_stop(bus: phydev->sfp_bus); | 
|---|
| 1656 |  | 
|---|
| 1657 | phydev->state = PHY_HALTED; | 
|---|
| 1658 | phy_process_state_change(phydev, old_state); | 
|---|
| 1659 |  | 
|---|
| 1660 | state_work = _phy_state_machine(phydev); | 
|---|
| 1661 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1662 |  | 
|---|
| 1663 | _phy_state_machine_post_work(phydev, state_work); | 
|---|
| 1664 | phy_stop_machine(phydev); | 
|---|
| 1665 |  | 
|---|
| 1666 | /* Cannot call flush_scheduled_work() here as desired because | 
|---|
| 1667 | * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler | 
|---|
| 1668 | * will not reenable interrupts. | 
|---|
| 1669 | */ | 
|---|
| 1670 | } | 
|---|
| 1671 | EXPORT_SYMBOL(phy_stop); | 
|---|
| 1672 |  | 
|---|
| 1673 | /** | 
|---|
| 1674 | * phy_start - start or restart a PHY device | 
|---|
| 1675 | * @phydev: target phy_device struct | 
|---|
| 1676 | * | 
|---|
| 1677 | * Description: Indicates the attached device's readiness to | 
|---|
| 1678 | *   handle PHY-related work.  Used during startup to start the | 
|---|
| 1679 | *   PHY, and after a call to phy_stop() to resume operation. | 
|---|
| 1680 | *   Also used to indicate the MDIO bus has cleared an error | 
|---|
| 1681 | *   condition. | 
|---|
| 1682 | */ | 
|---|
| 1683 | void phy_start(struct phy_device *phydev) | 
|---|
| 1684 | { | 
|---|
| 1685 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1686 |  | 
|---|
| 1687 | if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) { | 
|---|
| 1688 | WARN(1, "called from state %s\n", | 
|---|
| 1689 | phy_state_to_str(phydev->state)); | 
|---|
| 1690 | goto out; | 
|---|
| 1691 | } | 
|---|
| 1692 |  | 
|---|
| 1693 | if (phydev->sfp_bus) | 
|---|
| 1694 | sfp_upstream_start(bus: phydev->sfp_bus); | 
|---|
| 1695 |  | 
|---|
| 1696 | /* if phy was suspended, bring the physical link up again */ | 
|---|
| 1697 | __phy_resume(phydev); | 
|---|
| 1698 |  | 
|---|
| 1699 | phydev->state = PHY_UP; | 
|---|
| 1700 |  | 
|---|
| 1701 | phy_start_machine(phydev); | 
|---|
| 1702 | out: | 
|---|
| 1703 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1704 | } | 
|---|
| 1705 | EXPORT_SYMBOL(phy_start); | 
|---|
| 1706 |  | 
|---|
| 1707 | /** | 
|---|
| 1708 | * phy_mac_interrupt - MAC says the link has changed | 
|---|
| 1709 | * @phydev: phy_device struct with changed link | 
|---|
| 1710 | * | 
|---|
| 1711 | * The MAC layer is able to indicate there has been a change in the PHY link | 
|---|
| 1712 | * status. Trigger the state machine and work a work queue. | 
|---|
| 1713 | */ | 
|---|
| 1714 | void phy_mac_interrupt(struct phy_device *phydev) | 
|---|
| 1715 | { | 
|---|
| 1716 | /* Trigger a state machine change */ | 
|---|
| 1717 | phy_trigger_machine(phydev); | 
|---|
| 1718 | } | 
|---|
| 1719 | EXPORT_SYMBOL(phy_mac_interrupt); | 
|---|
| 1720 |  | 
|---|
| 1721 | /** | 
|---|
| 1722 | * phy_loopback - Configure loopback mode of PHY | 
|---|
| 1723 | * @phydev: target phy_device struct | 
|---|
| 1724 | * @enable: enable or disable loopback mode | 
|---|
| 1725 | * @speed: enable loopback mode with speed | 
|---|
| 1726 | * | 
|---|
| 1727 | * Configure loopback mode of PHY and signal link down and link up if speed is | 
|---|
| 1728 | * changing. | 
|---|
| 1729 | * | 
|---|
| 1730 | * Return: 0 on success, negative error code on failure. | 
|---|
| 1731 | */ | 
|---|
| 1732 | int phy_loopback(struct phy_device *phydev, bool enable, int speed) | 
|---|
| 1733 | { | 
|---|
| 1734 | bool link_up = false; | 
|---|
| 1735 | int ret = 0; | 
|---|
| 1736 |  | 
|---|
| 1737 | if (!phydev->drv) | 
|---|
| 1738 | return -EIO; | 
|---|
| 1739 |  | 
|---|
| 1740 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1741 |  | 
|---|
| 1742 | if (enable && phydev->loopback_enabled) { | 
|---|
| 1743 | ret = -EBUSY; | 
|---|
| 1744 | goto out; | 
|---|
| 1745 | } | 
|---|
| 1746 |  | 
|---|
| 1747 | if (!enable && !phydev->loopback_enabled) { | 
|---|
| 1748 | ret = -EINVAL; | 
|---|
| 1749 | goto out; | 
|---|
| 1750 | } | 
|---|
| 1751 |  | 
|---|
| 1752 | if (enable) { | 
|---|
| 1753 | /* | 
|---|
| 1754 | * Link up is signaled with a defined speed. If speed changes, | 
|---|
| 1755 | * then first link down and after that link up needs to be | 
|---|
| 1756 | * signaled. | 
|---|
| 1757 | */ | 
|---|
| 1758 | if (phydev->link && phydev->state == PHY_RUNNING) { | 
|---|
| 1759 | /* link is up and signaled */ | 
|---|
| 1760 | if (speed && phydev->speed != speed) { | 
|---|
| 1761 | /* signal link down and up for new speed */ | 
|---|
| 1762 | phydev->link = false; | 
|---|
| 1763 | phydev->state = PHY_NOLINK; | 
|---|
| 1764 | phy_link_down(phydev); | 
|---|
| 1765 |  | 
|---|
| 1766 | link_up = true; | 
|---|
| 1767 | } | 
|---|
| 1768 | } else { | 
|---|
| 1769 | /* link is not signaled */ | 
|---|
| 1770 | if (speed) { | 
|---|
| 1771 | /* signal link up for new speed */ | 
|---|
| 1772 | link_up = true; | 
|---|
| 1773 | } | 
|---|
| 1774 | } | 
|---|
| 1775 | } | 
|---|
| 1776 |  | 
|---|
| 1777 | if (phydev->drv->set_loopback) | 
|---|
| 1778 | ret = phydev->drv->set_loopback(phydev, enable, speed); | 
|---|
| 1779 | else | 
|---|
| 1780 | ret = genphy_loopback(phydev, enable, speed); | 
|---|
| 1781 |  | 
|---|
| 1782 | if (ret) { | 
|---|
| 1783 | if (enable) { | 
|---|
| 1784 | /* try to restore link if enabling loopback fails */ | 
|---|
| 1785 | if (phydev->drv->set_loopback) | 
|---|
| 1786 | phydev->drv->set_loopback(phydev, false, 0); | 
|---|
| 1787 | else | 
|---|
| 1788 | genphy_loopback(phydev, enable: false, speed: 0); | 
|---|
| 1789 | } | 
|---|
| 1790 |  | 
|---|
| 1791 | goto out; | 
|---|
| 1792 | } | 
|---|
| 1793 |  | 
|---|
| 1794 | if (link_up) { | 
|---|
| 1795 | phydev->link = true; | 
|---|
| 1796 | phydev->state = PHY_RUNNING; | 
|---|
| 1797 | phy_link_up(phydev); | 
|---|
| 1798 | } | 
|---|
| 1799 |  | 
|---|
| 1800 | phydev->loopback_enabled = enable; | 
|---|
| 1801 |  | 
|---|
| 1802 | out: | 
|---|
| 1803 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1804 | return ret; | 
|---|
| 1805 | } | 
|---|
| 1806 | EXPORT_SYMBOL(phy_loopback); | 
|---|
| 1807 |  | 
|---|
| 1808 | /** | 
|---|
| 1809 | * phy_eee_tx_clock_stop_capable() - indicate whether the MAC can stop tx clock | 
|---|
| 1810 | * @phydev: target phy_device struct | 
|---|
| 1811 | * | 
|---|
| 1812 | * Indicate whether the MAC can disable the transmit xMII clock while in LPI | 
|---|
| 1813 | * state. Returns 1 if the MAC may stop the transmit clock, 0 if the MAC must | 
|---|
| 1814 | * not stop the transmit clock, or negative error. | 
|---|
| 1815 | */ | 
|---|
| 1816 | int phy_eee_tx_clock_stop_capable(struct phy_device *phydev) | 
|---|
| 1817 | { | 
|---|
| 1818 | int stat1; | 
|---|
| 1819 |  | 
|---|
| 1820 | stat1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1); | 
|---|
| 1821 | if (stat1 < 0) | 
|---|
| 1822 | return stat1; | 
|---|
| 1823 |  | 
|---|
| 1824 | return !!(stat1 & MDIO_PCS_STAT1_CLKSTOP_CAP); | 
|---|
| 1825 | } | 
|---|
| 1826 | EXPORT_SYMBOL_GPL(phy_eee_tx_clock_stop_capable); | 
|---|
| 1827 |  | 
|---|
| 1828 | /** | 
|---|
| 1829 | * phy_eee_rx_clock_stop() - configure PHY receive clock in LPI | 
|---|
| 1830 | * @phydev: target phy_device struct | 
|---|
| 1831 | * @clk_stop_enable: flag to indicate whether the clock can be stopped | 
|---|
| 1832 | * | 
|---|
| 1833 | * Configure whether the PHY can disable its receive clock during LPI mode, | 
|---|
| 1834 | * See IEEE 802.3 sections 22.2.2.2, 35.2.2.10, and 45.2.3.1.4. | 
|---|
| 1835 | * | 
|---|
| 1836 | * Returns: 0 or negative error. | 
|---|
| 1837 | */ | 
|---|
| 1838 | int phy_eee_rx_clock_stop(struct phy_device *phydev, bool clk_stop_enable) | 
|---|
| 1839 | { | 
|---|
| 1840 | /* Configure the PHY to stop receiving xMII | 
|---|
| 1841 | * clock while it is signaling LPI. | 
|---|
| 1842 | */ | 
|---|
| 1843 | return phy_modify_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, | 
|---|
| 1844 | MDIO_PCS_CTRL1_CLKSTOP_EN, | 
|---|
| 1845 | set: clk_stop_enable ? MDIO_PCS_CTRL1_CLKSTOP_EN : 0); | 
|---|
| 1846 | } | 
|---|
| 1847 | EXPORT_SYMBOL_GPL(phy_eee_rx_clock_stop); | 
|---|
| 1848 |  | 
|---|
| 1849 | /** | 
|---|
| 1850 | * phy_init_eee - init and check the EEE feature | 
|---|
| 1851 | * @phydev: target phy_device struct | 
|---|
| 1852 | * @clk_stop_enable: PHY may stop the clock during LPI | 
|---|
| 1853 | * | 
|---|
| 1854 | * Description: it checks if the Energy-Efficient Ethernet (EEE) | 
|---|
| 1855 | * is supported by looking at the MMD registers 3.20 and 7.60/61 | 
|---|
| 1856 | * and it programs the MMD register 3.0 setting the "Clock stop enable" | 
|---|
| 1857 | * bit if required. | 
|---|
| 1858 | */ | 
|---|
| 1859 | int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable) | 
|---|
| 1860 | { | 
|---|
| 1861 | int ret; | 
|---|
| 1862 |  | 
|---|
| 1863 | if (!phydev->drv) | 
|---|
| 1864 | return -EIO; | 
|---|
| 1865 |  | 
|---|
| 1866 | ret = genphy_c45_eee_is_active(phydev, NULL); | 
|---|
| 1867 | if (ret < 0) | 
|---|
| 1868 | return ret; | 
|---|
| 1869 | if (!ret) | 
|---|
| 1870 | return -EPROTONOSUPPORT; | 
|---|
| 1871 |  | 
|---|
| 1872 | if (clk_stop_enable) | 
|---|
| 1873 | ret = phy_eee_rx_clock_stop(phydev, true); | 
|---|
| 1874 |  | 
|---|
| 1875 | return ret < 0 ? ret : 0; | 
|---|
| 1876 | } | 
|---|
| 1877 | EXPORT_SYMBOL(phy_init_eee); | 
|---|
| 1878 |  | 
|---|
| 1879 | /** | 
|---|
| 1880 | * phy_get_eee_err - report the EEE wake error count | 
|---|
| 1881 | * @phydev: target phy_device struct | 
|---|
| 1882 | * | 
|---|
| 1883 | * Description: it is to report the number of time where the PHY | 
|---|
| 1884 | * failed to complete its normal wake sequence. | 
|---|
| 1885 | */ | 
|---|
| 1886 | int phy_get_eee_err(struct phy_device *phydev) | 
|---|
| 1887 | { | 
|---|
| 1888 | int ret; | 
|---|
| 1889 |  | 
|---|
| 1890 | if (!phydev->drv) | 
|---|
| 1891 | return -EIO; | 
|---|
| 1892 |  | 
|---|
| 1893 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1894 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR); | 
|---|
| 1895 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1896 |  | 
|---|
| 1897 | return ret; | 
|---|
| 1898 | } | 
|---|
| 1899 | EXPORT_SYMBOL(phy_get_eee_err); | 
|---|
| 1900 |  | 
|---|
| 1901 | /** | 
|---|
| 1902 | * phy_ethtool_get_eee - get EEE supported and status | 
|---|
| 1903 | * @phydev: target phy_device struct | 
|---|
| 1904 | * @data: ethtool_keee data | 
|---|
| 1905 | * | 
|---|
| 1906 | * Description: get the current EEE settings, filling in all members of | 
|---|
| 1907 | * @data. | 
|---|
| 1908 | */ | 
|---|
| 1909 | int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_keee *data) | 
|---|
| 1910 | { | 
|---|
| 1911 | int ret; | 
|---|
| 1912 |  | 
|---|
| 1913 | if (!phydev->drv) | 
|---|
| 1914 | return -EIO; | 
|---|
| 1915 |  | 
|---|
| 1916 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1917 | ret = genphy_c45_ethtool_get_eee(phydev, data); | 
|---|
| 1918 | eeecfg_to_eee(eee: data, eeecfg: &phydev->eee_cfg); | 
|---|
| 1919 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1920 |  | 
|---|
| 1921 | return ret; | 
|---|
| 1922 | } | 
|---|
| 1923 | EXPORT_SYMBOL(phy_ethtool_get_eee); | 
|---|
| 1924 |  | 
|---|
| 1925 | /** | 
|---|
| 1926 | * phy_ethtool_set_eee_noneg - Adjusts MAC LPI configuration without PHY | 
|---|
| 1927 | *			       renegotiation | 
|---|
| 1928 | * @phydev: pointer to the target PHY device structure | 
|---|
| 1929 | * @old_cfg: pointer to the eee_config structure containing the old EEE settings | 
|---|
| 1930 | * | 
|---|
| 1931 | * This function updates the Energy Efficient Ethernet (EEE) configuration | 
|---|
| 1932 | * for cases where only the MAC's Low Power Idle (LPI) configuration changes, | 
|---|
| 1933 | * without triggering PHY renegotiation. It ensures that the MAC is properly | 
|---|
| 1934 | * informed of the new LPI settings by cycling the link down and up, which | 
|---|
| 1935 | * is necessary for the MAC to adopt the new configuration. This adjustment | 
|---|
| 1936 | * is done only if there is a change in the tx_lpi_enabled or tx_lpi_timer | 
|---|
| 1937 | * configuration. | 
|---|
| 1938 | */ | 
|---|
| 1939 | static void phy_ethtool_set_eee_noneg(struct phy_device *phydev, | 
|---|
| 1940 | const struct eee_config *old_cfg) | 
|---|
| 1941 | { | 
|---|
| 1942 | bool enable_tx_lpi; | 
|---|
| 1943 |  | 
|---|
| 1944 | if (!phydev->link) | 
|---|
| 1945 | return; | 
|---|
| 1946 |  | 
|---|
| 1947 | enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled && phydev->eee_active; | 
|---|
| 1948 |  | 
|---|
| 1949 | if (phydev->enable_tx_lpi != enable_tx_lpi || | 
|---|
| 1950 | phydev->eee_cfg.tx_lpi_timer != old_cfg->tx_lpi_timer) { | 
|---|
| 1951 | phydev->enable_tx_lpi = false; | 
|---|
| 1952 | phydev->link = false; | 
|---|
| 1953 | phy_link_down(phydev); | 
|---|
| 1954 | phydev->enable_tx_lpi = enable_tx_lpi; | 
|---|
| 1955 | phydev->link = true; | 
|---|
| 1956 | phy_link_up(phydev); | 
|---|
| 1957 | } | 
|---|
| 1958 | } | 
|---|
| 1959 |  | 
|---|
| 1960 | /** | 
|---|
| 1961 | * phy_ethtool_set_eee - set EEE supported and status | 
|---|
| 1962 | * @phydev: target phy_device struct | 
|---|
| 1963 | * @data: ethtool_keee data | 
|---|
| 1964 | * | 
|---|
| 1965 | * Description: it is to program the Advertisement EEE register. | 
|---|
| 1966 | */ | 
|---|
| 1967 | int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_keee *data) | 
|---|
| 1968 | { | 
|---|
| 1969 | struct eee_config old_cfg; | 
|---|
| 1970 | int ret; | 
|---|
| 1971 |  | 
|---|
| 1972 | if (!phydev->drv) | 
|---|
| 1973 | return -EIO; | 
|---|
| 1974 |  | 
|---|
| 1975 | mutex_lock(lock: &phydev->lock); | 
|---|
| 1976 |  | 
|---|
| 1977 | old_cfg = phydev->eee_cfg; | 
|---|
| 1978 | eee_to_eeecfg(eeecfg: &phydev->eee_cfg, eee: data); | 
|---|
| 1979 |  | 
|---|
| 1980 | ret = genphy_c45_ethtool_set_eee(phydev, data); | 
|---|
| 1981 | if (ret == 0) | 
|---|
| 1982 | phy_ethtool_set_eee_noneg(phydev, old_cfg: &old_cfg); | 
|---|
| 1983 | else if (ret < 0) | 
|---|
| 1984 | phydev->eee_cfg = old_cfg; | 
|---|
| 1985 |  | 
|---|
| 1986 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 1987 |  | 
|---|
| 1988 | return ret < 0 ? ret : 0; | 
|---|
| 1989 | } | 
|---|
| 1990 | EXPORT_SYMBOL(phy_ethtool_set_eee); | 
|---|
| 1991 |  | 
|---|
| 1992 | /** | 
|---|
| 1993 | * phy_ethtool_set_wol - Configure Wake On LAN | 
|---|
| 1994 | * | 
|---|
| 1995 | * @phydev: target phy_device struct | 
|---|
| 1996 | * @wol: Configuration requested | 
|---|
| 1997 | */ | 
|---|
| 1998 | int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) | 
|---|
| 1999 | { | 
|---|
| 2000 | int ret; | 
|---|
| 2001 |  | 
|---|
| 2002 | if (phydev->drv && phydev->drv->set_wol) { | 
|---|
| 2003 | mutex_lock(lock: &phydev->lock); | 
|---|
| 2004 | ret = phydev->drv->set_wol(phydev, wol); | 
|---|
| 2005 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 2006 |  | 
|---|
| 2007 | return ret; | 
|---|
| 2008 | } | 
|---|
| 2009 |  | 
|---|
| 2010 | return -EOPNOTSUPP; | 
|---|
| 2011 | } | 
|---|
| 2012 | EXPORT_SYMBOL(phy_ethtool_set_wol); | 
|---|
| 2013 |  | 
|---|
| 2014 | /** | 
|---|
| 2015 | * phy_ethtool_get_wol - Get the current Wake On LAN configuration | 
|---|
| 2016 | * | 
|---|
| 2017 | * @phydev: target phy_device struct | 
|---|
| 2018 | * @wol: Store the current configuration here | 
|---|
| 2019 | */ | 
|---|
| 2020 | void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) | 
|---|
| 2021 | { | 
|---|
| 2022 | if (phydev->drv && phydev->drv->get_wol) { | 
|---|
| 2023 | mutex_lock(lock: &phydev->lock); | 
|---|
| 2024 | phydev->drv->get_wol(phydev, wol); | 
|---|
| 2025 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 2026 | } | 
|---|
| 2027 | } | 
|---|
| 2028 | EXPORT_SYMBOL(phy_ethtool_get_wol); | 
|---|
| 2029 |  | 
|---|
| 2030 | int phy_ethtool_get_link_ksettings(struct net_device *ndev, | 
|---|
| 2031 | struct ethtool_link_ksettings *cmd) | 
|---|
| 2032 | { | 
|---|
| 2033 | struct phy_device *phydev = ndev->phydev; | 
|---|
| 2034 |  | 
|---|
| 2035 | if (!phydev) | 
|---|
| 2036 | return -ENODEV; | 
|---|
| 2037 |  | 
|---|
| 2038 | phy_ethtool_ksettings_get(phydev, cmd); | 
|---|
| 2039 |  | 
|---|
| 2040 | return 0; | 
|---|
| 2041 | } | 
|---|
| 2042 | EXPORT_SYMBOL(phy_ethtool_get_link_ksettings); | 
|---|
| 2043 |  | 
|---|
| 2044 | int phy_ethtool_set_link_ksettings(struct net_device *ndev, | 
|---|
| 2045 | const struct ethtool_link_ksettings *cmd) | 
|---|
| 2046 | { | 
|---|
| 2047 | struct phy_device *phydev = ndev->phydev; | 
|---|
| 2048 |  | 
|---|
| 2049 | if (!phydev) | 
|---|
| 2050 | return -ENODEV; | 
|---|
| 2051 |  | 
|---|
| 2052 | return phy_ethtool_ksettings_set(phydev, cmd); | 
|---|
| 2053 | } | 
|---|
| 2054 | EXPORT_SYMBOL(phy_ethtool_set_link_ksettings); | 
|---|
| 2055 |  | 
|---|
| 2056 | /** | 
|---|
| 2057 | * phy_ethtool_nway_reset - Restart auto negotiation | 
|---|
| 2058 | * @ndev: Network device to restart autoneg for | 
|---|
| 2059 | */ | 
|---|
| 2060 | int phy_ethtool_nway_reset(struct net_device *ndev) | 
|---|
| 2061 | { | 
|---|
| 2062 | struct phy_device *phydev = ndev->phydev; | 
|---|
| 2063 | int ret; | 
|---|
| 2064 |  | 
|---|
| 2065 | if (!phydev) | 
|---|
| 2066 | return -ENODEV; | 
|---|
| 2067 |  | 
|---|
| 2068 | if (!phydev->drv) | 
|---|
| 2069 | return -EIO; | 
|---|
| 2070 |  | 
|---|
| 2071 | mutex_lock(lock: &phydev->lock); | 
|---|
| 2072 | ret = phy_restart_aneg(phydev); | 
|---|
| 2073 | mutex_unlock(lock: &phydev->lock); | 
|---|
| 2074 |  | 
|---|
| 2075 | return ret; | 
|---|
| 2076 | } | 
|---|
| 2077 | EXPORT_SYMBOL(phy_ethtool_nway_reset); | 
|---|
| 2078 |  | 
|---|