1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Framework and drivers for configuring and reading different PHYs 4 * Based on code in sungem_phy.c and (long-removed) gianfar_phy.c 5 * 6 * Author: Andy Fleming 7 * 8 * Copyright (c) 2004 Freescale Semiconductor, Inc. 9 */ 10 11 #ifndef __PHY_H 12 #define __PHY_H 13 14 #include <linux/compiler.h> 15 #include <linux/spinlock.h> 16 #include <linux/ethtool.h> 17 #include <linux/linkmode.h> 18 #include <linux/netlink.h> 19 #include <linux/mdio.h> 20 #include <linux/mii.h> 21 #include <linux/mii_timestamper.h> 22 #include <linux/module.h> 23 #include <linux/timer.h> 24 #include <linux/workqueue.h> 25 #include <linux/mod_devicetable.h> 26 #include <linux/u64_stats_sync.h> 27 #include <linux/irqreturn.h> 28 #include <linux/iopoll.h> 29 #include <linux/refcount.h> 30 31 #include <linux/atomic.h> 32 33 #define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \ 34 SUPPORTED_TP | \ 35 SUPPORTED_MII) 36 37 #define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \ 38 SUPPORTED_10baseT_Full) 39 40 #define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \ 41 SUPPORTED_100baseT_Full) 42 43 #define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \ 44 SUPPORTED_1000baseT_Full) 45 46 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init; 47 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init; 48 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init; 49 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init; 50 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init; 51 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init; 52 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init; 53 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init; 54 55 #define PHY_BASIC_FEATURES ((unsigned long *)&phy_basic_features) 56 #define PHY_BASIC_T1_FEATURES ((unsigned long *)&phy_basic_t1_features) 57 #define PHY_GBIT_FEATURES ((unsigned long *)&phy_gbit_features) 58 #define PHY_GBIT_FIBRE_FEATURES ((unsigned long *)&phy_gbit_fibre_features) 59 #define PHY_GBIT_ALL_PORTS_FEATURES ((unsigned long *)&phy_gbit_all_ports_features) 60 #define PHY_10GBIT_FEATURES ((unsigned long *)&phy_10gbit_features) 61 #define PHY_10GBIT_FEC_FEATURES ((unsigned long *)&phy_10gbit_fec_features) 62 #define PHY_10GBIT_FULL_FEATURES ((unsigned long *)&phy_10gbit_full_features) 63 64 extern const int phy_basic_ports_array[3]; 65 extern const int phy_fibre_port_array[1]; 66 extern const int phy_all_ports_features_array[7]; 67 extern const int phy_10_100_features_array[4]; 68 extern const int phy_basic_t1_features_array[2]; 69 extern const int phy_gbit_features_array[2]; 70 extern const int phy_10gbit_features_array[1]; 71 72 /* 73 * Set phydev->irq to PHY_POLL if interrupts are not supported, 74 * or not desired for this PHY. Set to PHY_IGNORE_INTERRUPT if 75 * the attached driver handles the interrupt 76 */ 77 #define PHY_POLL -1 78 #define PHY_IGNORE_INTERRUPT -2 79 80 #define PHY_IS_INTERNAL 0x00000001 81 #define PHY_RST_AFTER_CLK_EN 0x00000002 82 #define PHY_POLL_CABLE_TEST 0x00000004 83 #define MDIO_DEVICE_IS_PHY 0x80000000 84 85 /* Interface Mode definitions */ 86 typedef enum { 87 PHY_INTERFACE_MODE_NA, 88 PHY_INTERFACE_MODE_INTERNAL, 89 PHY_INTERFACE_MODE_MII, 90 PHY_INTERFACE_MODE_GMII, 91 PHY_INTERFACE_MODE_SGMII, 92 PHY_INTERFACE_MODE_TBI, 93 PHY_INTERFACE_MODE_REVMII, 94 PHY_INTERFACE_MODE_RMII, 95 PHY_INTERFACE_MODE_RGMII, 96 PHY_INTERFACE_MODE_RGMII_ID, 97 PHY_INTERFACE_MODE_RGMII_RXID, 98 PHY_INTERFACE_MODE_RGMII_TXID, 99 PHY_INTERFACE_MODE_RTBI, 100 PHY_INTERFACE_MODE_SMII, 101 PHY_INTERFACE_MODE_XGMII, 102 PHY_INTERFACE_MODE_XLGMII, 103 PHY_INTERFACE_MODE_MOCA, 104 PHY_INTERFACE_MODE_QSGMII, 105 PHY_INTERFACE_MODE_TRGMII, 106 PHY_INTERFACE_MODE_1000BASEX, 107 PHY_INTERFACE_MODE_2500BASEX, 108 PHY_INTERFACE_MODE_RXAUI, 109 PHY_INTERFACE_MODE_XAUI, 110 /* 10GBASE-R, XFI, SFI - single lane 10G Serdes */ 111 PHY_INTERFACE_MODE_10GBASER, 112 PHY_INTERFACE_MODE_USXGMII, 113 /* 10GBASE-KR - with Clause 73 AN */ 114 PHY_INTERFACE_MODE_10GKR, 115 PHY_INTERFACE_MODE_MAX, 116 } phy_interface_t; 117 118 /** 119 * phy_supported_speeds - return all speeds currently supported by a phy device 120 * @phy: The phy device to return supported speeds of. 121 * @speeds: buffer to store supported speeds in. 122 * @size: size of speeds buffer. 123 * 124 * Description: Returns the number of supported speeds, and fills 125 * the speeds buffer with the supported speeds. If speeds buffer is 126 * too small to contain all currently supported speeds, will return as 127 * many speeds as can fit. 128 */ 129 unsigned int phy_supported_speeds(struct phy_device *phy, 130 unsigned int *speeds, 131 unsigned int size); 132 133 /** 134 * phy_modes - map phy_interface_t enum to device tree binding of phy-mode 135 * @interface: enum phy_interface_t value 136 * 137 * Description: maps 'enum phy_interface_t' defined in this file 138 * into the device tree binding of 'phy-mode', so that Ethernet 139 * device driver can get phy interface from device tree. 140 */ 141 static inline const char *phy_modes(phy_interface_t interface) 142 { 143 switch (interface) { 144 case PHY_INTERFACE_MODE_NA: 145 return ""; 146 case PHY_INTERFACE_MODE_INTERNAL: 147 return "internal"; 148 case PHY_INTERFACE_MODE_MII: 149 return "mii"; 150 case PHY_INTERFACE_MODE_GMII: 151 return "gmii"; 152 case PHY_INTERFACE_MODE_SGMII: 153 return "sgmii"; 154 case PHY_INTERFACE_MODE_TBI: 155 return "tbi"; 156 case PHY_INTERFACE_MODE_REVMII: 157 return "rev-mii"; 158 case PHY_INTERFACE_MODE_RMII: 159 return "rmii"; 160 case PHY_INTERFACE_MODE_RGMII: 161 return "rgmii"; 162 case PHY_INTERFACE_MODE_RGMII_ID: 163 return "rgmii-id"; 164 case PHY_INTERFACE_MODE_RGMII_RXID: 165 return "rgmii-rxid"; 166 case PHY_INTERFACE_MODE_RGMII_TXID: 167 return "rgmii-txid"; 168 case PHY_INTERFACE_MODE_RTBI: 169 return "rtbi"; 170 case PHY_INTERFACE_MODE_SMII: 171 return "smii"; 172 case PHY_INTERFACE_MODE_XGMII: 173 return "xgmii"; 174 case PHY_INTERFACE_MODE_XLGMII: 175 return "xlgmii"; 176 case PHY_INTERFACE_MODE_MOCA: 177 return "moca"; 178 case PHY_INTERFACE_MODE_QSGMII: 179 return "qsgmii"; 180 case PHY_INTERFACE_MODE_TRGMII: 181 return "trgmii"; 182 case PHY_INTERFACE_MODE_1000BASEX: 183 return "1000base-x"; 184 case PHY_INTERFACE_MODE_2500BASEX: 185 return "2500base-x"; 186 case PHY_INTERFACE_MODE_RXAUI: 187 return "rxaui"; 188 case PHY_INTERFACE_MODE_XAUI: 189 return "xaui"; 190 case PHY_INTERFACE_MODE_10GBASER: 191 return "10gbase-r"; 192 case PHY_INTERFACE_MODE_USXGMII: 193 return "usxgmii"; 194 case PHY_INTERFACE_MODE_10GKR: 195 return "10gbase-kr"; 196 default: 197 return "unknown"; 198 } 199 } 200 201 202 #define PHY_INIT_TIMEOUT 100000 203 #define PHY_FORCE_TIMEOUT 10 204 205 #define PHY_MAX_ADDR 32 206 207 /* Used when trying to connect to a specific phy (mii bus id:phy device id) */ 208 #define PHY_ID_FMT "%s:%02x" 209 210 #define MII_BUS_ID_SIZE 61 211 212 /* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit 213 IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips. */ 214 #define MII_ADDR_C45 (1<<30) 215 #define MII_DEVADDR_C45_SHIFT 16 216 #define MII_REGADDR_C45_MASK GENMASK(15, 0) 217 218 struct device; 219 struct phylink; 220 struct sfp_bus; 221 struct sfp_upstream_ops; 222 struct sk_buff; 223 224 struct mdio_bus_stats { 225 u64_stats_t transfers; 226 u64_stats_t errors; 227 u64_stats_t writes; 228 u64_stats_t reads; 229 /* Must be last, add new statistics above */ 230 struct u64_stats_sync syncp; 231 }; 232 233 /* Represents a shared structure between different phydev's in the same 234 * package, for example a quad PHY. See phy_package_join() and 235 * phy_package_leave(). 236 */ 237 struct phy_package_shared { 238 int addr; 239 refcount_t refcnt; 240 unsigned long flags; 241 size_t priv_size; 242 243 /* private data pointer */ 244 /* note that this pointer is shared between different phydevs and 245 * the user has to take care of appropriate locking. It is allocated 246 * and freed automatically by phy_package_join() and 247 * phy_package_leave(). 248 */ 249 void *priv; 250 }; 251 252 /* used as bit number in atomic bitops */ 253 #define PHY_SHARED_F_INIT_DONE 0 254 255 /* 256 * The Bus class for PHYs. Devices which provide access to 257 * PHYs should register using this structure 258 */ 259 struct mii_bus { 260 struct module *owner; 261 const char *name; 262 char id[MII_BUS_ID_SIZE]; 263 void *priv; 264 int (*read)(struct mii_bus *bus, int addr, int regnum); 265 int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val); 266 int (*reset)(struct mii_bus *bus); 267 struct mdio_bus_stats stats[PHY_MAX_ADDR]; 268 269 unsigned int is_managed:1; /* is device-managed */ 270 unsigned int is_managed_registered:1; 271 272 /* 273 * A lock to ensure that only one thing can read/write 274 * the MDIO bus at a time 275 */ 276 struct mutex mdio_lock; 277 278 struct device *parent; 279 enum { 280 MDIOBUS_ALLOCATED = 1, 281 MDIOBUS_REGISTERED, 282 MDIOBUS_UNREGISTERED, 283 MDIOBUS_RELEASED, 284 } state; 285 struct device dev; 286 287 /* list of all PHYs on bus */ 288 struct mdio_device *mdio_map[PHY_MAX_ADDR]; 289 290 /* PHY addresses to be ignored when probing */ 291 u32 phy_mask; 292 293 /* PHY addresses to ignore the TA/read failure */ 294 u32 phy_ignore_ta_mask; 295 296 /* 297 * An array of interrupts, each PHY's interrupt at the index 298 * matching its address 299 */ 300 int irq[PHY_MAX_ADDR]; 301 302 /* GPIO reset pulse width in microseconds */ 303 int reset_delay_us; 304 /* RESET GPIO descriptor pointer */ 305 struct gpio_desc *reset_gpiod; 306 307 /* protect access to the shared element */ 308 struct mutex shared_lock; 309 310 /* shared state across different PHYs */ 311 struct phy_package_shared *shared[PHY_MAX_ADDR]; 312 }; 313 #define to_mii_bus(d) container_of(d, struct mii_bus, dev) 314 315 struct mii_bus *mdiobus_alloc_size(size_t); 316 static inline struct mii_bus *mdiobus_alloc(void) 317 { 318 return mdiobus_alloc_size(0); 319 } 320 321 int __mdiobus_register(struct mii_bus *bus, struct module *owner); 322 #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE) 323 static inline int devm_mdiobus_register(struct mii_bus *bus) 324 { 325 int ret; 326 327 if (!bus->is_managed) 328 return -EPERM; 329 330 ret = mdiobus_register(bus); 331 if (!ret) 332 bus->is_managed_registered = 1; 333 334 return ret; 335 } 336 337 void mdiobus_unregister(struct mii_bus *bus); 338 void mdiobus_free(struct mii_bus *bus); 339 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv); 340 static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev) 341 { 342 return devm_mdiobus_alloc_size(dev, 0); 343 } 344 345 struct mii_bus *mdio_find_bus(const char *mdio_name); 346 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus); 347 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr); 348 349 #define PHY_INTERRUPT_DISABLED false 350 #define PHY_INTERRUPT_ENABLED true 351 352 /* PHY state machine states: 353 * 354 * DOWN: PHY device and driver are not ready for anything. probe 355 * should be called if and only if the PHY is in this state, 356 * given that the PHY device exists. 357 * - PHY driver probe function will set the state to READY 358 * 359 * READY: PHY is ready to send and receive packets, but the 360 * controller is not. By default, PHYs which do not implement 361 * probe will be set to this state by phy_probe(). 362 * - start will set the state to UP 363 * 364 * UP: The PHY and attached device are ready to do work. 365 * Interrupts should be started here. 366 * - timer moves to NOLINK or RUNNING 367 * 368 * NOLINK: PHY is up, but not currently plugged in. 369 * - irq or timer will set RUNNING if link comes back 370 * - phy_stop moves to HALTED 371 * 372 * RUNNING: PHY is currently up, running, and possibly sending 373 * and/or receiving packets 374 * - irq or timer will set NOLINK if link goes down 375 * - phy_stop moves to HALTED 376 * 377 * CABLETEST: PHY is performing a cable test. Packet reception/sending 378 * is not expected to work, carrier will be indicated as down. PHY will be 379 * poll once per second, or on interrupt for it current state. 380 * Once complete, move to UP to restart the PHY. 381 * - phy_stop aborts the running test and moves to HALTED 382 * 383 * HALTED: PHY is up, but no polling or interrupts are done. Or 384 * PHY is in an error state. 385 * - phy_start moves to UP 386 */ 387 enum phy_state { 388 PHY_DOWN = 0, 389 PHY_READY, 390 PHY_HALTED, 391 PHY_UP, 392 PHY_RUNNING, 393 PHY_NOLINK, 394 PHY_CABLETEST, 395 }; 396 397 /** 398 * struct phy_c45_device_ids - 802.3-c45 Device Identifiers 399 * @devices_in_package: Bit vector of devices present. 400 * @device_ids: The device identifer for each present device. 401 */ 402 struct phy_c45_device_ids { 403 u32 devices_in_package; 404 u32 device_ids[8]; 405 }; 406 407 struct macsec_context; 408 struct macsec_ops; 409 410 /* phy_device: An instance of a PHY 411 * 412 * drv: Pointer to the driver for this PHY instance 413 * phy_id: UID for this device found during discovery 414 * c45_ids: 802.3-c45 Device Identifers if is_c45. 415 * is_c45: Set to true if this phy uses clause 45 addressing. 416 * is_internal: Set to true if this phy is internal to a MAC. 417 * is_pseudo_fixed_link: Set to true if this phy is an Ethernet switch, etc. 418 * is_gigabit_capable: Set to true if PHY supports 1000Mbps 419 * has_fixups: Set to true if this phy has fixups/quirks. 420 * suspended: Set to true if this phy has been suspended successfully. 421 * suspended_by_mdio_bus: Set to true if this phy was suspended by MDIO bus. 422 * sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal. 423 * loopback_enabled: Set true if this phy has been loopbacked successfully. 424 * downshifted_rate: Set true if link speed has been downshifted. 425 * state: state of the PHY for management purposes 426 * dev_flags: Device-specific flags used by the PHY driver. 427 * irq: IRQ number of the PHY's interrupt (-1 if none) 428 * phy_timer: The timer for handling the state machine 429 * sfp_bus_attached: flag indicating whether the SFP bus has been attached 430 * sfp_bus: SFP bus attached to this PHY's fiber port 431 * attached_dev: The attached enet driver's device instance ptr 432 * adjust_link: Callback for the enet controller to respond to 433 * changes in the link state. 434 * macsec_ops: MACsec offloading ops. 435 * 436 * speed, duplex, pause, supported, advertising, lp_advertising, 437 * and autoneg are used like in mii_if_info 438 * 439 * interrupts currently only supports enabled or disabled, 440 * but could be changed in the future to support enabling 441 * and disabling specific interrupts 442 * 443 * Contains some infrastructure for polling and interrupt 444 * handling, as well as handling shifts in PHY hardware state 445 */ 446 struct phy_device { 447 struct mdio_device mdio; 448 449 /* Information about the PHY type */ 450 /* And management functions */ 451 struct phy_driver *drv; 452 453 u32 phy_id; 454 455 struct phy_c45_device_ids c45_ids; 456 unsigned is_c45:1; 457 unsigned is_internal:1; 458 unsigned is_pseudo_fixed_link:1; 459 unsigned is_gigabit_capable:1; 460 unsigned has_fixups:1; 461 unsigned suspended:1; 462 unsigned suspended_by_mdio_bus:1; 463 unsigned sysfs_links:1; 464 unsigned loopback_enabled:1; 465 unsigned downshifted_rate:1; 466 467 unsigned autoneg:1; 468 /* The most recently read link state */ 469 unsigned link:1; 470 unsigned autoneg_complete:1; 471 472 /* Interrupts are enabled */ 473 unsigned interrupts:1; 474 475 enum phy_state state; 476 477 u32 dev_flags; 478 479 phy_interface_t interface; 480 481 /* 482 * forced speed & duplex (no autoneg) 483 * partner speed & duplex & pause (autoneg) 484 */ 485 int speed; 486 int duplex; 487 int pause; 488 int asym_pause; 489 u8 master_slave_get; 490 u8 master_slave_set; 491 u8 master_slave_state; 492 493 /* Union of PHY and Attached devices' supported link modes */ 494 /* See ethtool.h for more info */ 495 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); 496 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 497 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising); 498 /* used with phy_speed_down */ 499 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old); 500 501 /* Energy efficient ethernet modes which should be prohibited */ 502 u32 eee_broken_modes; 503 504 #ifdef CONFIG_LED_TRIGGER_PHY 505 struct phy_led_trigger *phy_led_triggers; 506 unsigned int phy_num_led_triggers; 507 struct phy_led_trigger *last_triggered; 508 509 struct phy_led_trigger *led_link_trigger; 510 #endif 511 512 /* 513 * Interrupt number for this PHY 514 * -1 means no interrupt 515 */ 516 int irq; 517 518 /* private data pointer */ 519 /* For use by PHYs to maintain extra state */ 520 void *priv; 521 522 /* shared data pointer */ 523 /* For use by PHYs inside the same package that need a shared state. */ 524 struct phy_package_shared *shared; 525 526 /* Reporting cable test results */ 527 struct sk_buff *skb; 528 void *ehdr; 529 struct nlattr *nest; 530 531 /* Interrupt and Polling infrastructure */ 532 struct delayed_work state_queue; 533 534 struct mutex lock; 535 536 /* This may be modified under the rtnl lock */ 537 bool sfp_bus_attached; 538 struct sfp_bus *sfp_bus; 539 struct phylink *phylink; 540 struct net_device *attached_dev; 541 struct mii_timestamper *mii_ts; 542 543 u8 mdix; 544 u8 mdix_ctrl; 545 546 void (*phy_link_change)(struct phy_device *phydev, bool up); 547 void (*adjust_link)(struct net_device *dev); 548 549 #if IS_ENABLED(CONFIG_MACSEC) 550 /* MACsec management functions */ 551 const struct macsec_ops *macsec_ops; 552 #endif 553 }; 554 #define to_phy_device(d) container_of(to_mdio_device(d), \ 555 struct phy_device, mdio) 556 557 /* struct phy_driver: Driver structure for a particular PHY type 558 * 559 * driver_data: static driver data 560 * phy_id: The result of reading the UID registers of this PHY 561 * type, and ANDing them with the phy_id_mask. This driver 562 * only works for PHYs with IDs which match this field 563 * name: The friendly name of this PHY type 564 * phy_id_mask: Defines the important bits of the phy_id 565 * features: A mandatory list of features (speed, duplex, etc) 566 * supported by this PHY 567 * flags: A bitfield defining certain other features this PHY 568 * supports (like interrupts) 569 * 570 * All functions are optional. If config_aneg or read_status 571 * are not implemented, the phy core uses the genphy versions. 572 * Note that none of these functions should be called from 573 * interrupt time. The goal is for the bus read/write functions 574 * to be able to block when the bus transaction is happening, 575 * and be freed up by an interrupt (The MPC85xx has this ability, 576 * though it is not currently supported in the driver). 577 */ 578 struct phy_driver { 579 struct mdio_driver_common mdiodrv; 580 u32 phy_id; 581 char *name; 582 u32 phy_id_mask; 583 const unsigned long * const features; 584 u32 flags; 585 const void *driver_data; 586 587 /* 588 * Called to issue a PHY software reset 589 */ 590 int (*soft_reset)(struct phy_device *phydev); 591 592 /* 593 * Called to initialize the PHY, 594 * including after a reset 595 */ 596 int (*config_init)(struct phy_device *phydev); 597 598 /* 599 * Called during discovery. Used to set 600 * up device-specific structures, if any 601 */ 602 int (*probe)(struct phy_device *phydev); 603 604 /* 605 * Probe the hardware to determine what abilities it has. 606 * Should only set phydev->supported. 607 */ 608 int (*get_features)(struct phy_device *phydev); 609 610 /* PHY Power Management */ 611 int (*suspend)(struct phy_device *phydev); 612 int (*resume)(struct phy_device *phydev); 613 614 /* 615 * Configures the advertisement and resets 616 * autonegotiation if phydev->autoneg is on, 617 * forces the speed to the current settings in phydev 618 * if phydev->autoneg is off 619 */ 620 int (*config_aneg)(struct phy_device *phydev); 621 622 /* Determines the auto negotiation result */ 623 int (*aneg_done)(struct phy_device *phydev); 624 625 /* Determines the negotiated speed and duplex */ 626 int (*read_status)(struct phy_device *phydev); 627 628 /* Clears any pending interrupts */ 629 int (*ack_interrupt)(struct phy_device *phydev); 630 631 /* Enables or disables interrupts */ 632 int (*config_intr)(struct phy_device *phydev); 633 634 /* 635 * Checks if the PHY generated an interrupt. 636 * For multi-PHY devices with shared PHY interrupt pin 637 * Set interrupt bits have to be cleared. 638 */ 639 int (*did_interrupt)(struct phy_device *phydev); 640 641 /* Override default interrupt handling */ 642 irqreturn_t (*handle_interrupt)(struct phy_device *phydev); 643 644 /* Clears up any memory if needed */ 645 void (*remove)(struct phy_device *phydev); 646 647 /* Returns true if this is a suitable driver for the given 648 * phydev. If NULL, matching is based on phy_id and 649 * phy_id_mask. 650 */ 651 int (*match_phy_device)(struct phy_device *phydev); 652 653 /* Some devices (e.g. qnap TS-119P II) require PHY register changes to 654 * enable Wake on LAN, so set_wol is provided to be called in the 655 * ethernet driver's set_wol function. */ 656 int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol); 657 658 /* See set_wol, but for checking whether Wake on LAN is enabled. */ 659 void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol); 660 661 /* 662 * Called to inform a PHY device driver when the core is about to 663 * change the link state. This callback is supposed to be used as 664 * fixup hook for drivers that need to take action when the link 665 * state changes. Drivers are by no means allowed to mess with the 666 * PHY device structure in their implementations. 667 */ 668 void (*link_change_notify)(struct phy_device *dev); 669 670 /* 671 * Phy specific driver override for reading a MMD register. 672 * This function is optional for PHY specific drivers. When 673 * not provided, the default MMD read function will be used 674 * by phy_read_mmd(), which will use either a direct read for 675 * Clause 45 PHYs or an indirect read for Clause 22 PHYs. 676 * devnum is the MMD device number within the PHY device, 677 * regnum is the register within the selected MMD device. 678 */ 679 int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum); 680 681 /* 682 * Phy specific driver override for writing a MMD register. 683 * This function is optional for PHY specific drivers. When 684 * not provided, the default MMD write function will be used 685 * by phy_write_mmd(), which will use either a direct write for 686 * Clause 45 PHYs, or an indirect write for Clause 22 PHYs. 687 * devnum is the MMD device number within the PHY device, 688 * regnum is the register within the selected MMD device. 689 * val is the value to be written. 690 */ 691 int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum, 692 u16 val); 693 694 int (*read_page)(struct phy_device *dev); 695 int (*write_page)(struct phy_device *dev, int page); 696 697 /* Get the size and type of the eeprom contained within a plug-in 698 * module */ 699 int (*module_info)(struct phy_device *dev, 700 struct ethtool_modinfo *modinfo); 701 702 /* Get the eeprom information from the plug-in module */ 703 int (*module_eeprom)(struct phy_device *dev, 704 struct ethtool_eeprom *ee, u8 *data); 705 706 /* Start a cable test */ 707 int (*cable_test_start)(struct phy_device *dev); 708 /* Once per second, or on interrupt, request the status of the 709 * test. 710 */ 711 int (*cable_test_get_status)(struct phy_device *dev, bool *finished); 712 713 /* Get statistics from the phy using ethtool */ 714 int (*get_sset_count)(struct phy_device *dev); 715 void (*get_strings)(struct phy_device *dev, u8 *data); 716 void (*get_stats)(struct phy_device *dev, 717 struct ethtool_stats *stats, u64 *data); 718 719 /* Get and Set PHY tunables */ 720 int (*get_tunable)(struct phy_device *dev, 721 struct ethtool_tunable *tuna, void *data); 722 int (*set_tunable)(struct phy_device *dev, 723 struct ethtool_tunable *tuna, 724 const void *data); 725 int (*set_loopback)(struct phy_device *dev, bool enable); 726 }; 727 #define to_phy_driver(d) container_of(to_mdio_common_driver(d), \ 728 struct phy_driver, mdiodrv) 729 730 #define PHY_ANY_ID "MATCH ANY PHY" 731 #define PHY_ANY_UID 0xffffffff 732 733 #define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 0) 734 #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4) 735 #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10) 736 737 /* A Structure for boards to register fixups with the PHY Lib */ 738 struct phy_fixup { 739 struct list_head list; 740 char bus_id[MII_BUS_ID_SIZE + 3]; 741 u32 phy_uid; 742 u32 phy_uid_mask; 743 int (*run)(struct phy_device *phydev); 744 }; 745 746 const char *phy_speed_to_str(int speed); 747 const char *phy_duplex_to_str(unsigned int duplex); 748 749 /* A structure for mapping a particular speed and duplex 750 * combination to a particular SUPPORTED and ADVERTISED value 751 */ 752 struct phy_setting { 753 u32 speed; 754 u8 duplex; 755 u8 bit; 756 }; 757 758 const struct phy_setting * 759 phy_lookup_setting(int speed, int duplex, const unsigned long *mask, 760 bool exact); 761 size_t phy_speeds(unsigned int *speeds, size_t size, 762 unsigned long *mask); 763 void of_set_phy_supported(struct phy_device *phydev); 764 void of_set_phy_eee_broken(struct phy_device *phydev); 765 int phy_speed_down_core(struct phy_device *phydev); 766 767 /** 768 * phy_is_started - Convenience function to check whether PHY is started 769 * @phydev: The phy_device struct 770 */ 771 static inline bool phy_is_started(struct phy_device *phydev) 772 { 773 return phydev->state >= PHY_UP; 774 } 775 776 void phy_resolve_aneg_pause(struct phy_device *phydev); 777 void phy_resolve_aneg_linkmode(struct phy_device *phydev); 778 void phy_check_downshift(struct phy_device *phydev); 779 780 /** 781 * phy_read - Convenience function for reading a given PHY register 782 * @phydev: the phy_device struct 783 * @regnum: register number to read 784 * 785 * NOTE: MUST NOT be called from interrupt context, 786 * because the bus read/write functions may wait for an interrupt 787 * to conclude the operation. 788 */ 789 static inline int phy_read(struct phy_device *phydev, u32 regnum) 790 { 791 return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum); 792 } 793 794 #define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \ 795 timeout_us, sleep_before_read) \ 796 ({ \ 797 int __ret = read_poll_timeout(phy_read, val, (cond) || val < 0, \ 798 sleep_us, timeout_us, sleep_before_read, phydev, regnum); \ 799 if (val < 0) \ 800 __ret = val; \ 801 if (__ret) \ 802 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \ 803 __ret; \ 804 }) 805 806 807 /** 808 * __phy_read - convenience function for reading a given PHY register 809 * @phydev: the phy_device struct 810 * @regnum: register number to read 811 * 812 * The caller must have taken the MDIO bus lock. 813 */ 814 static inline int __phy_read(struct phy_device *phydev, u32 regnum) 815 { 816 return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum); 817 } 818 819 /** 820 * phy_write - Convenience function for writing a given PHY register 821 * @phydev: the phy_device struct 822 * @regnum: register number to write 823 * @val: value to write to @regnum 824 * 825 * NOTE: MUST NOT be called from interrupt context, 826 * because the bus read/write functions may wait for an interrupt 827 * to conclude the operation. 828 */ 829 static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val) 830 { 831 return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val); 832 } 833 834 /** 835 * __phy_write - Convenience function for writing a given PHY register 836 * @phydev: the phy_device struct 837 * @regnum: register number to write 838 * @val: value to write to @regnum 839 * 840 * The caller must have taken the MDIO bus lock. 841 */ 842 static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val) 843 { 844 return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, 845 val); 846 } 847 848 /** 849 * __phy_modify_changed() - Convenience function for modifying a PHY register 850 * @phydev: a pointer to a &struct phy_device 851 * @regnum: register number 852 * @mask: bit mask of bits to clear 853 * @set: bit mask of bits to set 854 * 855 * Unlocked helper function which allows a PHY register to be modified as 856 * new register value = (old register value & ~mask) | set 857 * 858 * Returns negative errno, 0 if there was no change, and 1 in case of change 859 */ 860 static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum, 861 u16 mask, u16 set) 862 { 863 return __mdiobus_modify_changed(phydev->mdio.bus, phydev->mdio.addr, 864 regnum, mask, set); 865 } 866 867 /** 868 * phy_read_mmd - Convenience function for reading a register 869 * from an MMD on a given PHY. 870 * @phydev: The phy_device struct 871 * @devad: The MMD to read from 872 * @regnum: The register on the MMD to read 873 * 874 * Same rules as for phy_read(); 875 */ 876 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum); 877 878 #define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \ 879 sleep_us, timeout_us, sleep_before_read) \ 880 ({ \ 881 int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \ 882 sleep_us, timeout_us, sleep_before_read, \ 883 phydev, devaddr, regnum); \ 884 if (val < 0) \ 885 __ret = val; \ 886 if (__ret) \ 887 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \ 888 __ret; \ 889 }) 890 891 /** 892 * __phy_read_mmd - Convenience function for reading a register 893 * from an MMD on a given PHY. 894 * @phydev: The phy_device struct 895 * @devad: The MMD to read from 896 * @regnum: The register on the MMD to read 897 * 898 * Same rules as for __phy_read(); 899 */ 900 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum); 901 902 /** 903 * phy_write_mmd - Convenience function for writing a register 904 * on an MMD on a given PHY. 905 * @phydev: The phy_device struct 906 * @devad: The MMD to write to 907 * @regnum: The register on the MMD to read 908 * @val: value to write to @regnum 909 * 910 * Same rules as for phy_write(); 911 */ 912 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val); 913 914 /** 915 * __phy_write_mmd - Convenience function for writing a register 916 * on an MMD on a given PHY. 917 * @phydev: The phy_device struct 918 * @devad: The MMD to write to 919 * @regnum: The register on the MMD to read 920 * @val: value to write to @regnum 921 * 922 * Same rules as for __phy_write(); 923 */ 924 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val); 925 926 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, 927 u16 set); 928 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, 929 u16 set); 930 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set); 931 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set); 932 933 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum, 934 u16 mask, u16 set); 935 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum, 936 u16 mask, u16 set); 937 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum, 938 u16 mask, u16 set); 939 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum, 940 u16 mask, u16 set); 941 942 /** 943 * __phy_set_bits - Convenience function for setting bits in a PHY register 944 * @phydev: the phy_device struct 945 * @regnum: register number to write 946 * @val: bits to set 947 * 948 * The caller must have taken the MDIO bus lock. 949 */ 950 static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val) 951 { 952 return __phy_modify(phydev, regnum, 0, val); 953 } 954 955 /** 956 * __phy_clear_bits - Convenience function for clearing bits in a PHY register 957 * @phydev: the phy_device struct 958 * @regnum: register number to write 959 * @val: bits to clear 960 * 961 * The caller must have taken the MDIO bus lock. 962 */ 963 static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum, 964 u16 val) 965 { 966 return __phy_modify(phydev, regnum, val, 0); 967 } 968 969 /** 970 * phy_set_bits - Convenience function for setting bits in a PHY register 971 * @phydev: the phy_device struct 972 * @regnum: register number to write 973 * @val: bits to set 974 */ 975 static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val) 976 { 977 return phy_modify(phydev, regnum, 0, val); 978 } 979 980 /** 981 * phy_clear_bits - Convenience function for clearing bits in a PHY register 982 * @phydev: the phy_device struct 983 * @regnum: register number to write 984 * @val: bits to clear 985 */ 986 static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val) 987 { 988 return phy_modify(phydev, regnum, val, 0); 989 } 990 991 /** 992 * __phy_set_bits_mmd - Convenience function for setting bits in a register 993 * on MMD 994 * @phydev: the phy_device struct 995 * @devad: the MMD containing register to modify 996 * @regnum: register number to modify 997 * @val: bits to set 998 * 999 * The caller must have taken the MDIO bus lock. 1000 */ 1001 static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad, 1002 u32 regnum, u16 val) 1003 { 1004 return __phy_modify_mmd(phydev, devad, regnum, 0, val); 1005 } 1006 1007 /** 1008 * __phy_clear_bits_mmd - Convenience function for clearing bits in a register 1009 * on MMD 1010 * @phydev: the phy_device struct 1011 * @devad: the MMD containing register to modify 1012 * @regnum: register number to modify 1013 * @val: bits to clear 1014 * 1015 * The caller must have taken the MDIO bus lock. 1016 */ 1017 static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad, 1018 u32 regnum, u16 val) 1019 { 1020 return __phy_modify_mmd(phydev, devad, regnum, val, 0); 1021 } 1022 1023 /** 1024 * phy_set_bits_mmd - Convenience function for setting bits in a register 1025 * on MMD 1026 * @phydev: the phy_device struct 1027 * @devad: the MMD containing register to modify 1028 * @regnum: register number to modify 1029 * @val: bits to set 1030 */ 1031 static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad, 1032 u32 regnum, u16 val) 1033 { 1034 return phy_modify_mmd(phydev, devad, regnum, 0, val); 1035 } 1036 1037 /** 1038 * phy_clear_bits_mmd - Convenience function for clearing bits in a register 1039 * on MMD 1040 * @phydev: the phy_device struct 1041 * @devad: the MMD containing register to modify 1042 * @regnum: register number to modify 1043 * @val: bits to clear 1044 */ 1045 static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad, 1046 u32 regnum, u16 val) 1047 { 1048 return phy_modify_mmd(phydev, devad, regnum, val, 0); 1049 } 1050 1051 /** 1052 * phy_interrupt_is_valid - Convenience function for testing a given PHY irq 1053 * @phydev: the phy_device struct 1054 * 1055 * NOTE: must be kept in sync with addition/removal of PHY_POLL and 1056 * PHY_IGNORE_INTERRUPT 1057 */ 1058 static inline bool phy_interrupt_is_valid(struct phy_device *phydev) 1059 { 1060 return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT; 1061 } 1062 1063 /** 1064 * phy_polling_mode - Convenience function for testing whether polling is 1065 * used to detect PHY status changes 1066 * @phydev: the phy_device struct 1067 */ 1068 static inline bool phy_polling_mode(struct phy_device *phydev) 1069 { 1070 if (phydev->state == PHY_CABLETEST) 1071 if (phydev->drv->flags & PHY_POLL_CABLE_TEST) 1072 return true; 1073 1074 return phydev->irq == PHY_POLL; 1075 } 1076 1077 /** 1078 * phy_has_hwtstamp - Tests whether a PHY time stamp configuration. 1079 * @phydev: the phy_device struct 1080 */ 1081 static inline bool phy_has_hwtstamp(struct phy_device *phydev) 1082 { 1083 return phydev && phydev->mii_ts && phydev->mii_ts->hwtstamp; 1084 } 1085 1086 /** 1087 * phy_has_rxtstamp - Tests whether a PHY supports receive time stamping. 1088 * @phydev: the phy_device struct 1089 */ 1090 static inline bool phy_has_rxtstamp(struct phy_device *phydev) 1091 { 1092 return phydev && phydev->mii_ts && phydev->mii_ts->rxtstamp; 1093 } 1094 1095 /** 1096 * phy_has_tsinfo - Tests whether a PHY reports time stamping and/or 1097 * PTP hardware clock capabilities. 1098 * @phydev: the phy_device struct 1099 */ 1100 static inline bool phy_has_tsinfo(struct phy_device *phydev) 1101 { 1102 return phydev && phydev->mii_ts && phydev->mii_ts->ts_info; 1103 } 1104 1105 /** 1106 * phy_has_txtstamp - Tests whether a PHY supports transmit time stamping. 1107 * @phydev: the phy_device struct 1108 */ 1109 static inline bool phy_has_txtstamp(struct phy_device *phydev) 1110 { 1111 return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp; 1112 } 1113 1114 static inline int phy_hwtstamp(struct phy_device *phydev, struct ifreq *ifr) 1115 { 1116 return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr); 1117 } 1118 1119 static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb, 1120 int type) 1121 { 1122 return phydev->mii_ts->rxtstamp(phydev->mii_ts, skb, type); 1123 } 1124 1125 static inline int phy_ts_info(struct phy_device *phydev, 1126 struct ethtool_ts_info *tsinfo) 1127 { 1128 return phydev->mii_ts->ts_info(phydev->mii_ts, tsinfo); 1129 } 1130 1131 static inline void phy_txtstamp(struct phy_device *phydev, struct sk_buff *skb, 1132 int type) 1133 { 1134 phydev->mii_ts->txtstamp(phydev->mii_ts, skb, type); 1135 } 1136 1137 /** 1138 * phy_is_internal - Convenience function for testing if a PHY is internal 1139 * @phydev: the phy_device struct 1140 */ 1141 static inline bool phy_is_internal(struct phy_device *phydev) 1142 { 1143 return phydev->is_internal; 1144 } 1145 1146 /** 1147 * phy_interface_mode_is_rgmii - Convenience function for testing if a 1148 * PHY interface mode is RGMII (all variants) 1149 * @mode: the phy_interface_t enum 1150 */ 1151 static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode) 1152 { 1153 return mode >= PHY_INTERFACE_MODE_RGMII && 1154 mode <= PHY_INTERFACE_MODE_RGMII_TXID; 1155 }; 1156 1157 /** 1158 * phy_interface_mode_is_8023z() - does the phy interface mode use 802.3z 1159 * negotiation 1160 * @mode: one of &enum phy_interface_t 1161 * 1162 * Returns true if the phy interface mode uses the 16-bit negotiation 1163 * word as defined in 802.3z. (See 802.3-2015 37.2.1 Config_Reg encoding) 1164 */ 1165 static inline bool phy_interface_mode_is_8023z(phy_interface_t mode) 1166 { 1167 return mode == PHY_INTERFACE_MODE_1000BASEX || 1168 mode == PHY_INTERFACE_MODE_2500BASEX; 1169 } 1170 1171 /** 1172 * phy_interface_is_rgmii - Convenience function for testing if a PHY interface 1173 * is RGMII (all variants) 1174 * @phydev: the phy_device struct 1175 */ 1176 static inline bool phy_interface_is_rgmii(struct phy_device *phydev) 1177 { 1178 return phy_interface_mode_is_rgmii(phydev->interface); 1179 }; 1180 1181 /* 1182 * phy_is_pseudo_fixed_link - Convenience function for testing if this 1183 * PHY is the CPU port facing side of an Ethernet switch, or similar. 1184 * @phydev: the phy_device struct 1185 */ 1186 static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev) 1187 { 1188 return phydev->is_pseudo_fixed_link; 1189 } 1190 1191 int phy_save_page(struct phy_device *phydev); 1192 int phy_select_page(struct phy_device *phydev, int page); 1193 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret); 1194 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum); 1195 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val); 1196 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum, 1197 u16 mask, u16 set); 1198 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum, 1199 u16 mask, u16 set); 1200 1201 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id, 1202 bool is_c45, 1203 struct phy_c45_device_ids *c45_ids); 1204 #if IS_ENABLED(CONFIG_PHYLIB) 1205 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45); 1206 int phy_device_register(struct phy_device *phy); 1207 void phy_device_free(struct phy_device *phydev); 1208 #else 1209 static inline 1210 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45) 1211 { 1212 return NULL; 1213 } 1214 1215 static inline int phy_device_register(struct phy_device *phy) 1216 { 1217 return 0; 1218 } 1219 1220 static inline void phy_device_free(struct phy_device *phydev) { } 1221 #endif /* CONFIG_PHYLIB */ 1222 void phy_device_remove(struct phy_device *phydev); 1223 int phy_init_hw(struct phy_device *phydev); 1224 int phy_suspend(struct phy_device *phydev); 1225 int phy_resume(struct phy_device *phydev); 1226 int __phy_resume(struct phy_device *phydev); 1227 int phy_loopback(struct phy_device *phydev, bool enable); 1228 void phy_sfp_attach(void *upstream, struct sfp_bus *bus); 1229 void phy_sfp_detach(void *upstream, struct sfp_bus *bus); 1230 int phy_sfp_probe(struct phy_device *phydev, 1231 const struct sfp_upstream_ops *ops); 1232 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, 1233 phy_interface_t interface); 1234 struct phy_device *phy_find_first(struct mii_bus *bus); 1235 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, 1236 u32 flags, phy_interface_t interface); 1237 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, 1238 void (*handler)(struct net_device *), 1239 phy_interface_t interface); 1240 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id, 1241 void (*handler)(struct net_device *), 1242 phy_interface_t interface); 1243 void phy_disconnect(struct phy_device *phydev); 1244 void phy_detach(struct phy_device *phydev); 1245 void phy_start(struct phy_device *phydev); 1246 void phy_stop(struct phy_device *phydev); 1247 int phy_start_aneg(struct phy_device *phydev); 1248 int phy_aneg_done(struct phy_device *phydev); 1249 int phy_speed_down(struct phy_device *phydev, bool sync); 1250 int phy_speed_up(struct phy_device *phydev); 1251 1252 int phy_restart_aneg(struct phy_device *phydev); 1253 int phy_reset_after_clk_enable(struct phy_device *phydev); 1254 1255 #if IS_ENABLED(CONFIG_PHYLIB) 1256 int phy_start_cable_test(struct phy_device *phydev, 1257 struct netlink_ext_ack *extack); 1258 #else 1259 static inline 1260 int phy_start_cable_test(struct phy_device *phydev, 1261 struct netlink_ext_ack *extack) 1262 { 1263 NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support"); 1264 return -EOPNOTSUPP; 1265 } 1266 #endif 1267 1268 int phy_cable_test_result(struct phy_device *phydev, u8 pair, u16 result); 1269 int phy_cable_test_fault_length(struct phy_device *phydev, u8 pair, 1270 u16 cm); 1271 1272 static inline void phy_device_reset(struct phy_device *phydev, int value) 1273 { 1274 mdio_device_reset(&phydev->mdio, value); 1275 } 1276 1277 #define phydev_err(_phydev, format, args...) \ 1278 dev_err(&_phydev->mdio.dev, format, ##args) 1279 1280 #define phydev_info(_phydev, format, args...) \ 1281 dev_info(&_phydev->mdio.dev, format, ##args) 1282 1283 #define phydev_warn(_phydev, format, args...) \ 1284 dev_warn(&_phydev->mdio.dev, format, ##args) 1285 1286 #define phydev_dbg(_phydev, format, args...) \ 1287 dev_dbg(&_phydev->mdio.dev, format, ##args) 1288 1289 static inline const char *phydev_name(const struct phy_device *phydev) 1290 { 1291 return dev_name(&phydev->mdio.dev); 1292 } 1293 1294 static inline void phy_lock_mdio_bus(struct phy_device *phydev) 1295 { 1296 mutex_lock(&phydev->mdio.bus->mdio_lock); 1297 } 1298 1299 static inline void phy_unlock_mdio_bus(struct phy_device *phydev) 1300 { 1301 mutex_unlock(&phydev->mdio.bus->mdio_lock); 1302 } 1303 1304 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) 1305 __printf(2, 3); 1306 char *phy_attached_info_irq(struct phy_device *phydev) 1307 __malloc; 1308 void phy_attached_info(struct phy_device *phydev); 1309 1310 /* Clause 22 PHY */ 1311 int genphy_read_abilities(struct phy_device *phydev); 1312 int genphy_setup_forced(struct phy_device *phydev); 1313 int genphy_restart_aneg(struct phy_device *phydev); 1314 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart); 1315 int genphy_config_eee_advert(struct phy_device *phydev); 1316 int __genphy_config_aneg(struct phy_device *phydev, bool changed); 1317 int genphy_aneg_done(struct phy_device *phydev); 1318 int genphy_update_link(struct phy_device *phydev); 1319 int genphy_read_lpa(struct phy_device *phydev); 1320 int genphy_read_status_fixed(struct phy_device *phydev); 1321 int genphy_read_status(struct phy_device *phydev); 1322 int genphy_suspend(struct phy_device *phydev); 1323 int genphy_resume(struct phy_device *phydev); 1324 int genphy_loopback(struct phy_device *phydev, bool enable); 1325 int genphy_soft_reset(struct phy_device *phydev); 1326 1327 static inline int genphy_config_aneg(struct phy_device *phydev) 1328 { 1329 return __genphy_config_aneg(phydev, false); 1330 } 1331 1332 static inline int genphy_no_ack_interrupt(struct phy_device *phydev) 1333 { 1334 return 0; 1335 } 1336 static inline int genphy_no_config_intr(struct phy_device *phydev) 1337 { 1338 return 0; 1339 } 1340 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, 1341 u16 regnum); 1342 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, 1343 u16 regnum, u16 val); 1344 1345 /* Clause 37 */ 1346 int genphy_c37_config_aneg(struct phy_device *phydev); 1347 int genphy_c37_read_status(struct phy_device *phydev); 1348 1349 /* Clause 45 PHY */ 1350 int genphy_c45_restart_aneg(struct phy_device *phydev); 1351 int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart); 1352 int genphy_c45_aneg_done(struct phy_device *phydev); 1353 int genphy_c45_read_link(struct phy_device *phydev); 1354 int genphy_c45_read_lpa(struct phy_device *phydev); 1355 int genphy_c45_read_pma(struct phy_device *phydev); 1356 int genphy_c45_pma_setup_forced(struct phy_device *phydev); 1357 int genphy_c45_an_config_aneg(struct phy_device *phydev); 1358 int genphy_c45_an_disable_aneg(struct phy_device *phydev); 1359 int genphy_c45_read_mdix(struct phy_device *phydev); 1360 int genphy_c45_pma_read_abilities(struct phy_device *phydev); 1361 int genphy_c45_read_status(struct phy_device *phydev); 1362 int genphy_c45_config_aneg(struct phy_device *phydev); 1363 1364 /* The gen10g_* functions are the old Clause 45 stub */ 1365 int gen10g_config_aneg(struct phy_device *phydev); 1366 1367 static inline int phy_read_status(struct phy_device *phydev) 1368 { 1369 if (!phydev->drv) 1370 return -EIO; 1371 1372 if (phydev->drv->read_status) 1373 return phydev->drv->read_status(phydev); 1374 else 1375 return genphy_read_status(phydev); 1376 } 1377 1378 void phy_driver_unregister(struct phy_driver *drv); 1379 void phy_drivers_unregister(struct phy_driver *drv, int n); 1380 int phy_driver_register(struct phy_driver *new_driver, struct module *owner); 1381 int phy_drivers_register(struct phy_driver *new_driver, int n, 1382 struct module *owner); 1383 void phy_state_machine(struct work_struct *work); 1384 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies); 1385 void phy_mac_interrupt(struct phy_device *phydev); 1386 void phy_start_machine(struct phy_device *phydev); 1387 void phy_stop_machine(struct phy_device *phydev); 1388 void phy_ethtool_ksettings_get(struct phy_device *phydev, 1389 struct ethtool_link_ksettings *cmd); 1390 int phy_ethtool_ksettings_set(struct phy_device *phydev, 1391 const struct ethtool_link_ksettings *cmd); 1392 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd); 1393 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd); 1394 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd); 1395 void phy_request_interrupt(struct phy_device *phydev); 1396 void phy_free_interrupt(struct phy_device *phydev); 1397 void phy_print_status(struct phy_device *phydev); 1398 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed); 1399 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode); 1400 void phy_advertise_supported(struct phy_device *phydev); 1401 void phy_support_sym_pause(struct phy_device *phydev); 1402 void phy_support_asym_pause(struct phy_device *phydev); 1403 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, 1404 bool autoneg); 1405 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx); 1406 bool phy_validate_pause(struct phy_device *phydev, 1407 struct ethtool_pauseparam *pp); 1408 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause); 1409 void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv, 1410 bool *tx_pause, bool *rx_pause); 1411 1412 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask, 1413 int (*run)(struct phy_device *)); 1414 int phy_register_fixup_for_id(const char *bus_id, 1415 int (*run)(struct phy_device *)); 1416 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask, 1417 int (*run)(struct phy_device *)); 1418 1419 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask); 1420 int phy_unregister_fixup_for_id(const char *bus_id); 1421 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask); 1422 1423 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable); 1424 int phy_get_eee_err(struct phy_device *phydev); 1425 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data); 1426 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data); 1427 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol); 1428 void phy_ethtool_get_wol(struct phy_device *phydev, 1429 struct ethtool_wolinfo *wol); 1430 int phy_ethtool_get_link_ksettings(struct net_device *ndev, 1431 struct ethtool_link_ksettings *cmd); 1432 int phy_ethtool_set_link_ksettings(struct net_device *ndev, 1433 const struct ethtool_link_ksettings *cmd); 1434 int phy_ethtool_nway_reset(struct net_device *ndev); 1435 int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size); 1436 void phy_package_leave(struct phy_device *phydev); 1437 int devm_phy_package_join(struct device *dev, struct phy_device *phydev, 1438 int addr, size_t priv_size); 1439 1440 #if IS_ENABLED(CONFIG_PHYLIB) 1441 int __init mdio_bus_init(void); 1442 void mdio_bus_exit(void); 1443 #endif 1444 1445 /* Inline function for use within net/core/ethtool.c (built-in) */ 1446 static inline int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data) 1447 { 1448 if (!phydev->drv) 1449 return -EIO; 1450 1451 mutex_lock(&phydev->lock); 1452 phydev->drv->get_strings(phydev, data); 1453 mutex_unlock(&phydev->lock); 1454 1455 return 0; 1456 } 1457 1458 static inline int phy_ethtool_get_sset_count(struct phy_device *phydev) 1459 { 1460 int ret; 1461 1462 if (!phydev->drv) 1463 return -EIO; 1464 1465 if (phydev->drv->get_sset_count && 1466 phydev->drv->get_strings && 1467 phydev->drv->get_stats) { 1468 mutex_lock(&phydev->lock); 1469 ret = phydev->drv->get_sset_count(phydev); 1470 mutex_unlock(&phydev->lock); 1471 1472 return ret; 1473 } 1474 1475 return -EOPNOTSUPP; 1476 } 1477 1478 static inline int phy_ethtool_get_stats(struct phy_device *phydev, 1479 struct ethtool_stats *stats, u64 *data) 1480 { 1481 if (!phydev->drv) 1482 return -EIO; 1483 1484 mutex_lock(&phydev->lock); 1485 phydev->drv->get_stats(phydev, stats, data); 1486 mutex_unlock(&phydev->lock); 1487 1488 return 0; 1489 } 1490 1491 static inline int phy_package_read(struct phy_device *phydev, u32 regnum) 1492 { 1493 struct phy_package_shared *shared = phydev->shared; 1494 1495 if (!shared) 1496 return -EIO; 1497 1498 return mdiobus_read(phydev->mdio.bus, shared->addr, regnum); 1499 } 1500 1501 static inline int __phy_package_read(struct phy_device *phydev, u32 regnum) 1502 { 1503 struct phy_package_shared *shared = phydev->shared; 1504 1505 if (!shared) 1506 return -EIO; 1507 1508 return __mdiobus_read(phydev->mdio.bus, shared->addr, regnum); 1509 } 1510 1511 static inline int phy_package_write(struct phy_device *phydev, 1512 u32 regnum, u16 val) 1513 { 1514 struct phy_package_shared *shared = phydev->shared; 1515 1516 if (!shared) 1517 return -EIO; 1518 1519 return mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val); 1520 } 1521 1522 static inline int __phy_package_write(struct phy_device *phydev, 1523 u32 regnum, u16 val) 1524 { 1525 struct phy_package_shared *shared = phydev->shared; 1526 1527 if (!shared) 1528 return -EIO; 1529 1530 return __mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val); 1531 } 1532 1533 static inline bool phy_package_init_once(struct phy_device *phydev) 1534 { 1535 struct phy_package_shared *shared = phydev->shared; 1536 1537 if (!shared) 1538 return false; 1539 1540 return !test_and_set_bit(PHY_SHARED_F_INIT_DONE, &shared->flags); 1541 } 1542 1543 extern struct bus_type mdio_bus_type; 1544 1545 struct mdio_board_info { 1546 const char *bus_id; 1547 char modalias[MDIO_NAME_SIZE]; 1548 int mdio_addr; 1549 const void *platform_data; 1550 }; 1551 1552 #if IS_ENABLED(CONFIG_MDIO_DEVICE) 1553 int mdiobus_register_board_info(const struct mdio_board_info *info, 1554 unsigned int n); 1555 #else 1556 static inline int mdiobus_register_board_info(const struct mdio_board_info *i, 1557 unsigned int n) 1558 { 1559 return 0; 1560 } 1561 #endif 1562 1563 1564 /** 1565 * module_phy_driver() - Helper macro for registering PHY drivers 1566 * @__phy_drivers: array of PHY drivers to register 1567 * 1568 * Helper macro for PHY drivers which do not do anything special in module 1569 * init/exit. Each module may only use this macro once, and calling it 1570 * replaces module_init() and module_exit(). 1571 */ 1572 #define phy_module_driver(__phy_drivers, __count) \ 1573 static int __init phy_module_init(void) \ 1574 { \ 1575 return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \ 1576 } \ 1577 module_init(phy_module_init); \ 1578 static void __exit phy_module_exit(void) \ 1579 { \ 1580 phy_drivers_unregister(__phy_drivers, __count); \ 1581 } \ 1582 module_exit(phy_module_exit) 1583 1584 #define module_phy_driver(__phy_drivers) \ 1585 phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers)) 1586 1587 bool phy_driver_is_genphy(struct phy_device *phydev); 1588 bool phy_driver_is_genphy_10g(struct phy_device *phydev); 1589 1590 #endif /* __PHY_H */ 1591