| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* |
| 3 | * linux/mdio.h: definitions for MDIO (clause 45) transceivers |
| 4 | * Copyright 2006-2009 Solarflare Communications Inc. |
| 5 | */ |
| 6 | #ifndef __LINUX_MDIO_H__ |
| 7 | #define __LINUX_MDIO_H__ |
| 8 | |
| 9 | #include <uapi/linux/mdio.h> |
| 10 | #include <linux/bitfield.h> |
| 11 | #include <linux/mod_devicetable.h> |
| 12 | |
| 13 | struct gpio_desc; |
| 14 | struct mii_bus; |
| 15 | struct reset_control; |
| 16 | |
| 17 | /* Multiple levels of nesting are possible. However typically this is |
| 18 | * limited to nested DSA like layer, a MUX layer, and the normal |
| 19 | * user. Instead of trying to handle the general case, just define |
| 20 | * these cases. |
| 21 | */ |
| 22 | enum mdio_mutex_lock_class { |
| 23 | MDIO_MUTEX_NORMAL, |
| 24 | MDIO_MUTEX_MUX, |
| 25 | MDIO_MUTEX_NESTED, |
| 26 | }; |
| 27 | |
| 28 | struct mdio_device { |
| 29 | struct device dev; |
| 30 | |
| 31 | struct mii_bus *bus; |
| 32 | char modalias[MDIO_NAME_SIZE]; |
| 33 | |
| 34 | int (*bus_match)(struct device *dev, const struct device_driver *drv); |
| 35 | void (*device_free)(struct mdio_device *mdiodev); |
| 36 | void (*device_remove)(struct mdio_device *mdiodev); |
| 37 | |
| 38 | /* Bus address of the MDIO device (0-31) */ |
| 39 | int addr; |
| 40 | int flags; |
| 41 | int reset_state; |
| 42 | struct gpio_desc *reset_gpio; |
| 43 | struct reset_control *reset_ctrl; |
| 44 | unsigned int reset_assert_delay; |
| 45 | unsigned int reset_deassert_delay; |
| 46 | }; |
| 47 | |
| 48 | #define to_mdio_device(__dev) container_of_const(__dev, struct mdio_device, dev) |
| 49 | |
| 50 | /* struct mdio_driver_common: Common to all MDIO drivers */ |
| 51 | struct mdio_driver_common { |
| 52 | struct device_driver driver; |
| 53 | int flags; |
| 54 | }; |
| 55 | #define MDIO_DEVICE_FLAG_PHY 1 |
| 56 | |
| 57 | #define to_mdio_common_driver(__drv_c) container_of_const(__drv_c, struct mdio_driver_common, \ |
| 58 | driver) |
| 59 | |
| 60 | /* struct mdio_driver: Generic MDIO driver */ |
| 61 | struct mdio_driver { |
| 62 | struct mdio_driver_common mdiodrv; |
| 63 | |
| 64 | /* |
| 65 | * Called during discovery. Used to set |
| 66 | * up device-specific structures, if any |
| 67 | */ |
| 68 | int (*probe)(struct mdio_device *mdiodev); |
| 69 | |
| 70 | /* Clears up any memory if needed */ |
| 71 | void (*remove)(struct mdio_device *mdiodev); |
| 72 | |
| 73 | /* Quiesces the device on system shutdown, turns off interrupts etc */ |
| 74 | void (*shutdown)(struct mdio_device *mdiodev); |
| 75 | }; |
| 76 | |
| 77 | #define to_mdio_driver(__drv_m) container_of_const(to_mdio_common_driver(__drv_m), \ |
| 78 | struct mdio_driver, mdiodrv) |
| 79 | |
| 80 | /* device driver data */ |
| 81 | static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data) |
| 82 | { |
| 83 | dev_set_drvdata(dev: &mdio->dev, data); |
| 84 | } |
| 85 | |
| 86 | static inline void *mdiodev_get_drvdata(struct mdio_device *mdio) |
| 87 | { |
| 88 | return dev_get_drvdata(dev: &mdio->dev); |
| 89 | } |
| 90 | |
| 91 | void mdio_device_free(struct mdio_device *mdiodev); |
| 92 | struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr); |
| 93 | int mdio_device_register(struct mdio_device *mdiodev); |
| 94 | void mdio_device_remove(struct mdio_device *mdiodev); |
| 95 | void mdio_device_reset(struct mdio_device *mdiodev, int value); |
| 96 | int mdio_driver_register(struct mdio_driver *drv); |
| 97 | void mdio_driver_unregister(struct mdio_driver *drv); |
| 98 | int mdio_device_bus_match(struct device *dev, const struct device_driver *drv); |
| 99 | |
| 100 | static inline void mdio_device_get(struct mdio_device *mdiodev) |
| 101 | { |
| 102 | get_device(dev: &mdiodev->dev); |
| 103 | } |
| 104 | |
| 105 | static inline void mdio_device_put(struct mdio_device *mdiodev) |
| 106 | { |
| 107 | mdio_device_free(mdiodev); |
| 108 | } |
| 109 | |
| 110 | static inline bool mdio_phy_id_is_c45(int phy_id) |
| 111 | { |
| 112 | return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK); |
| 113 | } |
| 114 | |
| 115 | static inline __u16 mdio_phy_id_prtad(int phy_id) |
| 116 | { |
| 117 | return (phy_id & MDIO_PHY_ID_PRTAD) >> 5; |
| 118 | } |
| 119 | |
| 120 | static inline __u16 mdio_phy_id_devad(int phy_id) |
| 121 | { |
| 122 | return phy_id & MDIO_PHY_ID_DEVAD; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * struct mdio_if_info - Ethernet controller MDIO interface |
| 127 | * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown) |
| 128 | * @mmds: Mask of MMDs expected to be present in the PHY. This must be |
| 129 | * non-zero unless @prtad = %MDIO_PRTAD_NONE. |
| 130 | * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then |
| 131 | * MII register access will be passed through with @devad = |
| 132 | * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to |
| 133 | * commonly used clause 22 registers will be translated into |
| 134 | * clause 45 registers. |
| 135 | * @dev: Net device structure |
| 136 | * @mdio_read: Register read function; returns value or negative error code |
| 137 | * @mdio_write: Register write function; returns 0 or negative error code |
| 138 | */ |
| 139 | struct mdio_if_info { |
| 140 | int prtad; |
| 141 | u32 mmds; |
| 142 | unsigned mode_support; |
| 143 | |
| 144 | struct net_device *dev; |
| 145 | int (*mdio_read)(struct net_device *dev, int prtad, int devad, |
| 146 | u16 addr); |
| 147 | int (*mdio_write)(struct net_device *dev, int prtad, int devad, |
| 148 | u16 addr, u16 val); |
| 149 | }; |
| 150 | |
| 151 | #define MDIO_PRTAD_NONE (-1) |
| 152 | #define MDIO_DEVAD_NONE (-1) |
| 153 | #define MDIO_SUPPORTS_C22 1 |
| 154 | #define MDIO_SUPPORTS_C45 2 |
| 155 | #define MDIO_EMULATE_C22 4 |
| 156 | |
| 157 | struct ethtool_cmd; |
| 158 | struct ethtool_pauseparam; |
| 159 | extern int mdio45_probe(struct mdio_if_info *mdio, int prtad); |
| 160 | extern int mdio_set_flag(const struct mdio_if_info *mdio, |
| 161 | int prtad, int devad, u16 addr, int mask, |
| 162 | bool sense); |
| 163 | extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds); |
| 164 | extern int mdio45_nway_restart(const struct mdio_if_info *mdio); |
| 165 | extern void |
| 166 | mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio, |
| 167 | struct ethtool_link_ksettings *cmd, |
| 168 | u32 npage_adv, u32 npage_lpa); |
| 169 | |
| 170 | /** |
| 171 | * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS |
| 172 | * @mdio: MDIO interface |
| 173 | * @cmd: Ethtool request structure |
| 174 | * |
| 175 | * Since the CSRs for auto-negotiation using next pages are not fully |
| 176 | * standardised, this function does not attempt to decode them. Use |
| 177 | * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits |
| 178 | * from next pages. |
| 179 | */ |
| 180 | static inline void |
| 181 | mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio, |
| 182 | struct ethtool_link_ksettings *cmd) |
| 183 | { |
| 184 | mdio45_ethtool_ksettings_get_npage(mdio, cmd, npage_adv: 0, npage_lpa: 0); |
| 185 | } |
| 186 | |
| 187 | extern int mdio_mii_ioctl(const struct mdio_if_info *mdio, |
| 188 | struct mii_ioctl_data *mii_data, int cmd); |
| 189 | |
| 190 | /** |
| 191 | * mmd_eee_cap_to_ethtool_sup_t |
| 192 | * @eee_cap: value of the MMD EEE Capability register |
| 193 | * |
| 194 | * A small helper function that translates MMD EEE Capability (3.20) bits |
| 195 | * to ethtool supported settings. |
| 196 | */ |
| 197 | static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap) |
| 198 | { |
| 199 | u32 supported = 0; |
| 200 | |
| 201 | if (eee_cap & MDIO_EEE_100TX) |
| 202 | supported |= SUPPORTED_100baseT_Full; |
| 203 | if (eee_cap & MDIO_EEE_1000T) |
| 204 | supported |= SUPPORTED_1000baseT_Full; |
| 205 | if (eee_cap & MDIO_EEE_10GT) |
| 206 | supported |= SUPPORTED_10000baseT_Full; |
| 207 | if (eee_cap & MDIO_EEE_1000KX) |
| 208 | supported |= SUPPORTED_1000baseKX_Full; |
| 209 | if (eee_cap & MDIO_EEE_10GKX4) |
| 210 | supported |= SUPPORTED_10000baseKX4_Full; |
| 211 | if (eee_cap & MDIO_EEE_10GKR) |
| 212 | supported |= SUPPORTED_10000baseKR_Full; |
| 213 | |
| 214 | return supported; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * mmd_eee_adv_to_ethtool_adv_t |
| 219 | * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers |
| 220 | * |
| 221 | * A small helper function that translates the MMD EEE Advertisment (7.60) |
| 222 | * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement |
| 223 | * settings. |
| 224 | */ |
| 225 | static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv) |
| 226 | { |
| 227 | u32 adv = 0; |
| 228 | |
| 229 | if (eee_adv & MDIO_EEE_100TX) |
| 230 | adv |= ADVERTISED_100baseT_Full; |
| 231 | if (eee_adv & MDIO_EEE_1000T) |
| 232 | adv |= ADVERTISED_1000baseT_Full; |
| 233 | if (eee_adv & MDIO_EEE_10GT) |
| 234 | adv |= ADVERTISED_10000baseT_Full; |
| 235 | if (eee_adv & MDIO_EEE_1000KX) |
| 236 | adv |= ADVERTISED_1000baseKX_Full; |
| 237 | if (eee_adv & MDIO_EEE_10GKX4) |
| 238 | adv |= ADVERTISED_10000baseKX4_Full; |
| 239 | if (eee_adv & MDIO_EEE_10GKR) |
| 240 | adv |= ADVERTISED_10000baseKR_Full; |
| 241 | |
| 242 | return adv; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * ethtool_adv_to_mmd_eee_adv_t |
| 247 | * @adv: the ethtool advertisement settings |
| 248 | * |
| 249 | * A small helper function that translates ethtool advertisement settings |
| 250 | * to EEE advertisements for the MMD EEE Advertisement (7.60) and |
| 251 | * MMD EEE Link Partner Ability (7.61) registers. |
| 252 | */ |
| 253 | static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv) |
| 254 | { |
| 255 | u16 reg = 0; |
| 256 | |
| 257 | if (adv & ADVERTISED_100baseT_Full) |
| 258 | reg |= MDIO_EEE_100TX; |
| 259 | if (adv & ADVERTISED_1000baseT_Full) |
| 260 | reg |= MDIO_EEE_1000T; |
| 261 | if (adv & ADVERTISED_10000baseT_Full) |
| 262 | reg |= MDIO_EEE_10GT; |
| 263 | if (adv & ADVERTISED_1000baseKX_Full) |
| 264 | reg |= MDIO_EEE_1000KX; |
| 265 | if (adv & ADVERTISED_10000baseKX4_Full) |
| 266 | reg |= MDIO_EEE_10GKX4; |
| 267 | if (adv & ADVERTISED_10000baseKR_Full) |
| 268 | reg |= MDIO_EEE_10GKR; |
| 269 | |
| 270 | return reg; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * linkmode_adv_to_mii_10gbt_adv_t |
| 275 | * @advertising: the linkmode advertisement settings |
| 276 | * |
| 277 | * A small helper function that translates linkmode advertisement |
| 278 | * settings to phy autonegotiation advertisements for the C45 |
| 279 | * 10GBASE-T AN CONTROL (7.32) register. |
| 280 | */ |
| 281 | static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising) |
| 282 | { |
| 283 | u32 result = 0; |
| 284 | |
| 285 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, |
| 286 | advertising)) |
| 287 | result |= MDIO_AN_10GBT_CTRL_ADV2_5G; |
| 288 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, |
| 289 | advertising)) |
| 290 | result |= MDIO_AN_10GBT_CTRL_ADV5G; |
| 291 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, |
| 292 | advertising)) |
| 293 | result |= MDIO_AN_10GBT_CTRL_ADV10G; |
| 294 | |
| 295 | return result; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * mii_10gbt_stat_mod_linkmode_lpa_t |
| 300 | * @advertising: target the linkmode advertisement settings |
| 301 | * @lpa: value of the C45 10GBASE-T AN STATUS register |
| 302 | * |
| 303 | * A small helper function that translates C45 10GBASE-T AN STATUS register bits |
| 304 | * to linkmode advertisement settings. Other bits in advertising aren't changed. |
| 305 | */ |
| 306 | static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising, |
| 307 | u32 lpa) |
| 308 | { |
| 309 | linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, |
| 310 | advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G); |
| 311 | linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, |
| 312 | advertising, lpa & MDIO_AN_10GBT_STAT_LP5G); |
| 313 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, |
| 314 | advertising, lpa & MDIO_AN_10GBT_STAT_LP10G); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * mii_t1_adv_l_mod_linkmode_t |
| 319 | * @advertising: target the linkmode advertisement settings |
| 320 | * @lpa: value of the BASE-T1 Autonegotiation Advertisement [15:0] Register |
| 321 | * |
| 322 | * A small helper function that translates BASE-T1 Autonegotiation |
| 323 | * Advertisement [15:0] Register bits to linkmode advertisement settings. |
| 324 | * Other bits in advertising aren't changed. |
| 325 | */ |
| 326 | static inline void mii_t1_adv_l_mod_linkmode_t(unsigned long *advertising, u32 lpa) |
| 327 | { |
| 328 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising, |
| 329 | lpa & MDIO_AN_T1_ADV_L_PAUSE_CAP); |
| 330 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising, |
| 331 | lpa & MDIO_AN_T1_ADV_L_PAUSE_ASYM); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * mii_t1_adv_m_mod_linkmode_t |
| 336 | * @advertising: target the linkmode advertisement settings |
| 337 | * @lpa: value of the BASE-T1 Autonegotiation Advertisement [31:16] Register |
| 338 | * |
| 339 | * A small helper function that translates BASE-T1 Autonegotiation |
| 340 | * Advertisement [31:16] Register bits to linkmode advertisement settings. |
| 341 | * Other bits in advertising aren't changed. |
| 342 | */ |
| 343 | static inline void mii_t1_adv_m_mod_linkmode_t(unsigned long *advertising, u32 lpa) |
| 344 | { |
| 345 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, |
| 346 | advertising, lpa & MDIO_AN_T1_ADV_M_B10L); |
| 347 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT, |
| 348 | advertising, lpa & MDIO_AN_T1_ADV_M_100BT1); |
| 349 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT, |
| 350 | advertising, lpa & MDIO_AN_T1_ADV_M_1000BT1); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * linkmode_adv_to_mii_t1_adv_l_t |
| 355 | * @advertising: the linkmode advertisement settings |
| 356 | * |
| 357 | * A small helper function that translates linkmode advertisement |
| 358 | * settings to phy autonegotiation advertisements for the |
| 359 | * BASE-T1 Autonegotiation Advertisement [15:0] Register. |
| 360 | */ |
| 361 | static inline u32 linkmode_adv_to_mii_t1_adv_l_t(unsigned long *advertising) |
| 362 | { |
| 363 | u32 result = 0; |
| 364 | |
| 365 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising)) |
| 366 | result |= MDIO_AN_T1_ADV_L_PAUSE_CAP; |
| 367 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising)) |
| 368 | result |= MDIO_AN_T1_ADV_L_PAUSE_ASYM; |
| 369 | |
| 370 | return result; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * linkmode_adv_to_mii_t1_adv_m_t |
| 375 | * @advertising: the linkmode advertisement settings |
| 376 | * |
| 377 | * A small helper function that translates linkmode advertisement |
| 378 | * settings to phy autonegotiation advertisements for the |
| 379 | * BASE-T1 Autonegotiation Advertisement [31:16] Register. |
| 380 | */ |
| 381 | static inline u32 linkmode_adv_to_mii_t1_adv_m_t(unsigned long *advertising) |
| 382 | { |
| 383 | u32 result = 0; |
| 384 | |
| 385 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, advertising)) |
| 386 | result |= MDIO_AN_T1_ADV_M_B10L; |
| 387 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT, advertising)) |
| 388 | result |= MDIO_AN_T1_ADV_M_100BT1; |
| 389 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT, advertising)) |
| 390 | result |= MDIO_AN_T1_ADV_M_1000BT1; |
| 391 | |
| 392 | return result; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * mii_eee_cap1_mod_linkmode_t() |
| 397 | * @adv: target the linkmode advertisement settings |
| 398 | * @val: register value |
| 399 | * |
| 400 | * A function that translates value of following registers to the linkmode: |
| 401 | * IEEE 802.3-2018 45.2.3.10 "EEE control and capability 1" register (3.20) |
| 402 | * IEEE 802.3-2018 45.2.7.13 "EEE advertisement 1" register (7.60) |
| 403 | * IEEE 802.3-2018 45.2.7.14 "EEE link partner ability 1" register (7.61) |
| 404 | */ |
| 405 | static inline void mii_eee_cap1_mod_linkmode_t(unsigned long *adv, u32 val) |
| 406 | { |
| 407 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 408 | adv, val & MDIO_EEE_100TX); |
| 409 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 410 | adv, val & MDIO_EEE_1000T); |
| 411 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, |
| 412 | adv, val & MDIO_EEE_10GT); |
| 413 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, |
| 414 | adv, val & MDIO_EEE_1000KX); |
| 415 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, |
| 416 | adv, val & MDIO_EEE_10GKX4); |
| 417 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, |
| 418 | adv, val & MDIO_EEE_10GKR); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * mii_eee_cap2_mod_linkmode_sup_t() |
| 423 | * @adv: target the linkmode settings |
| 424 | * @val: register value |
| 425 | * |
| 426 | * A function that translates value of following registers to the linkmode: |
| 427 | * IEEE 802.3-2022 45.2.3.11 "EEE control and capability 2" register (3.21) |
| 428 | */ |
| 429 | static inline void mii_eee_cap2_mod_linkmode_sup_t(unsigned long *adv, u32 val) |
| 430 | { |
| 431 | linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, |
| 432 | adv, val & MDIO_EEE_2_5GT); |
| 433 | linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, |
| 434 | adv, val & MDIO_EEE_5GT); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * mii_eee_cap2_mod_linkmode_adv_t() |
| 439 | * @adv: target the linkmode advertisement settings |
| 440 | * @val: register value |
| 441 | * |
| 442 | * A function that translates value of following registers to the linkmode: |
| 443 | * IEEE 802.3-2022 45.2.7.16 "EEE advertisement 2" register (7.62) |
| 444 | * IEEE 802.3-2022 45.2.7.17 "EEE link partner ability 2" register (7.63) |
| 445 | * Note: Currently this function is the same as mii_eee_cap2_mod_linkmode_sup_t. |
| 446 | * For certain, not yet supported, modes however the bits differ. |
| 447 | * Therefore create separate functions already. |
| 448 | */ |
| 449 | static inline void mii_eee_cap2_mod_linkmode_adv_t(unsigned long *adv, u32 val) |
| 450 | { |
| 451 | linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, |
| 452 | adv, val & MDIO_EEE_2_5GT); |
| 453 | linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, |
| 454 | adv, val & MDIO_EEE_5GT); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * linkmode_to_mii_eee_cap1_t() |
| 459 | * @adv: the linkmode advertisement settings |
| 460 | * |
| 461 | * A function that translates linkmode to value for IEEE 802.3-2018 45.2.7.13 |
| 462 | * "EEE advertisement 1" register (7.60) |
| 463 | */ |
| 464 | static inline u32 linkmode_to_mii_eee_cap1_t(unsigned long *adv) |
| 465 | { |
| 466 | u32 result = 0; |
| 467 | |
| 468 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, adv)) |
| 469 | result |= MDIO_EEE_100TX; |
| 470 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, adv)) |
| 471 | result |= MDIO_EEE_1000T; |
| 472 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, adv)) |
| 473 | result |= MDIO_EEE_10GT; |
| 474 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, adv)) |
| 475 | result |= MDIO_EEE_1000KX; |
| 476 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, adv)) |
| 477 | result |= MDIO_EEE_10GKX4; |
| 478 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, adv)) |
| 479 | result |= MDIO_EEE_10GKR; |
| 480 | |
| 481 | return result; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * linkmode_to_mii_eee_cap2_t() |
| 486 | * @adv: the linkmode advertisement settings |
| 487 | * |
| 488 | * A function that translates linkmode to value for IEEE 802.3-2022 45.2.7.16 |
| 489 | * "EEE advertisement 2" register (7.62) |
| 490 | */ |
| 491 | static inline u32 linkmode_to_mii_eee_cap2_t(unsigned long *adv) |
| 492 | { |
| 493 | u32 result = 0; |
| 494 | |
| 495 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, adv)) |
| 496 | result |= MDIO_EEE_2_5GT; |
| 497 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, adv)) |
| 498 | result |= MDIO_EEE_5GT; |
| 499 | |
| 500 | return result; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * mii_10base_t1_adv_mod_linkmode_t() |
| 505 | * @adv: linkmode advertisement settings |
| 506 | * @val: register value |
| 507 | * |
| 508 | * A function that translates IEEE 802.3cg-2019 45.2.7.26 "10BASE-T1 AN status" |
| 509 | * register (7.527) value to the linkmode. |
| 510 | */ |
| 511 | static inline void mii_10base_t1_adv_mod_linkmode_t(unsigned long *adv, u16 val) |
| 512 | { |
| 513 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, |
| 514 | adv, val & MDIO_AN_10BT1_AN_CTRL_ADV_EEE_T1L); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * linkmode_adv_to_mii_10base_t1_t() |
| 519 | * @adv: linkmode advertisement settings |
| 520 | * |
| 521 | * A function that translates the linkmode to IEEE 802.3cg-2019 45.2.7.25 |
| 522 | * "10BASE-T1 AN control" register (7.526) value. |
| 523 | */ |
| 524 | static inline u32 linkmode_adv_to_mii_10base_t1_t(unsigned long *adv) |
| 525 | { |
| 526 | u32 result = 0; |
| 527 | |
| 528 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, adv)) |
| 529 | result |= MDIO_AN_10BT1_AN_CTRL_ADV_EEE_T1L; |
| 530 | |
| 531 | return result; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * mii_c73_mod_linkmode - convert a Clause 73 advertisement to linkmodes |
| 536 | * @adv: linkmode advertisement setting |
| 537 | * @lpa: array of three u16s containing the advertisement |
| 538 | * |
| 539 | * Convert an IEEE 802.3 Clause 73 advertisement to ethtool link modes. |
| 540 | */ |
| 541 | static inline void mii_c73_mod_linkmode(unsigned long *adv, u16 *lpa) |
| 542 | { |
| 543 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 544 | adv, lpa[0] & MDIO_AN_C73_0_PAUSE); |
| 545 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 546 | adv, lpa[0] & MDIO_AN_C73_0_ASM_DIR); |
| 547 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, |
| 548 | adv, lpa[1] & MDIO_AN_C73_1_1000BASE_KX); |
| 549 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, |
| 550 | adv, lpa[1] & MDIO_AN_C73_1_10GBASE_KX4); |
| 551 | linkmode_mod_bit(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, |
| 552 | adv, lpa[1] & MDIO_AN_C73_1_40GBASE_KR4); |
| 553 | linkmode_mod_bit(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, |
| 554 | adv, lpa[1] & MDIO_AN_C73_1_40GBASE_CR4); |
| 555 | /* 100GBASE_CR10 and 100GBASE_KP4 not implemented */ |
| 556 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, |
| 557 | adv, lpa[1] & MDIO_AN_C73_1_100GBASE_KR4); |
| 558 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, |
| 559 | adv, lpa[1] & MDIO_AN_C73_1_100GBASE_CR4); |
| 560 | /* 25GBASE_R_S not implemented */ |
| 561 | /* The 25GBASE_R bit can be used for 25Gbase KR or CR modes */ |
| 562 | linkmode_mod_bit(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, |
| 563 | adv, lpa[1] & MDIO_AN_C73_1_25GBASE_R); |
| 564 | linkmode_mod_bit(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, |
| 565 | adv, lpa[1] & MDIO_AN_C73_1_25GBASE_R); |
| 566 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, |
| 567 | adv, lpa[1] & MDIO_AN_C73_1_10GBASE_KR); |
| 568 | linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, |
| 569 | adv, lpa[2] & MDIO_AN_C73_2_2500BASE_KX); |
| 570 | /* 5GBASE_KR not implemented */ |
| 571 | } |
| 572 | |
| 573 | int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum); |
| 574 | int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val); |
| 575 | int __mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, |
| 576 | u16 set); |
| 577 | int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum, |
| 578 | u16 mask, u16 set); |
| 579 | |
| 580 | int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum); |
| 581 | int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum); |
| 582 | int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val); |
| 583 | int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val); |
| 584 | int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, |
| 585 | u16 set); |
| 586 | int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum, |
| 587 | u16 mask, u16 set); |
| 588 | int __mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum); |
| 589 | int mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum); |
| 590 | int mdiobus_c45_read_nested(struct mii_bus *bus, int addr, int devad, |
| 591 | u32 regnum); |
| 592 | int __mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum, |
| 593 | u16 val); |
| 594 | int mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum, |
| 595 | u16 val); |
| 596 | int mdiobus_c45_write_nested(struct mii_bus *bus, int addr, int devad, |
| 597 | u32 regnum, u16 val); |
| 598 | int mdiobus_c45_modify(struct mii_bus *bus, int addr, int devad, u32 regnum, |
| 599 | u16 mask, u16 set); |
| 600 | |
| 601 | int mdiobus_c45_modify_changed(struct mii_bus *bus, int addr, int devad, |
| 602 | u32 regnum, u16 mask, u16 set); |
| 603 | |
| 604 | static inline int __mdiodev_read(struct mdio_device *mdiodev, u32 regnum) |
| 605 | { |
| 606 | return __mdiobus_read(bus: mdiodev->bus, addr: mdiodev->addr, regnum); |
| 607 | } |
| 608 | |
| 609 | static inline int __mdiodev_write(struct mdio_device *mdiodev, u32 regnum, |
| 610 | u16 val) |
| 611 | { |
| 612 | return __mdiobus_write(bus: mdiodev->bus, addr: mdiodev->addr, regnum, val); |
| 613 | } |
| 614 | |
| 615 | static inline int __mdiodev_modify(struct mdio_device *mdiodev, u32 regnum, |
| 616 | u16 mask, u16 set) |
| 617 | { |
| 618 | return __mdiobus_modify(bus: mdiodev->bus, addr: mdiodev->addr, regnum, mask, set); |
| 619 | } |
| 620 | |
| 621 | static inline int __mdiodev_modify_changed(struct mdio_device *mdiodev, |
| 622 | u32 regnum, u16 mask, u16 set) |
| 623 | { |
| 624 | return __mdiobus_modify_changed(bus: mdiodev->bus, addr: mdiodev->addr, regnum, |
| 625 | mask, set); |
| 626 | } |
| 627 | |
| 628 | static inline int mdiodev_read(struct mdio_device *mdiodev, u32 regnum) |
| 629 | { |
| 630 | return mdiobus_read(bus: mdiodev->bus, addr: mdiodev->addr, regnum); |
| 631 | } |
| 632 | |
| 633 | static inline int mdiodev_write(struct mdio_device *mdiodev, u32 regnum, |
| 634 | u16 val) |
| 635 | { |
| 636 | return mdiobus_write(bus: mdiodev->bus, addr: mdiodev->addr, regnum, val); |
| 637 | } |
| 638 | |
| 639 | static inline int mdiodev_modify(struct mdio_device *mdiodev, u32 regnum, |
| 640 | u16 mask, u16 set) |
| 641 | { |
| 642 | return mdiobus_modify(bus: mdiodev->bus, addr: mdiodev->addr, regnum, mask, set); |
| 643 | } |
| 644 | |
| 645 | static inline int mdiodev_modify_changed(struct mdio_device *mdiodev, |
| 646 | u32 regnum, u16 mask, u16 set) |
| 647 | { |
| 648 | return mdiobus_modify_changed(bus: mdiodev->bus, addr: mdiodev->addr, regnum, |
| 649 | mask, set); |
| 650 | } |
| 651 | |
| 652 | static inline int mdiodev_c45_modify(struct mdio_device *mdiodev, int devad, |
| 653 | u32 regnum, u16 mask, u16 set) |
| 654 | { |
| 655 | return mdiobus_c45_modify(bus: mdiodev->bus, addr: mdiodev->addr, devad, regnum, |
| 656 | mask, set); |
| 657 | } |
| 658 | |
| 659 | static inline int mdiodev_c45_modify_changed(struct mdio_device *mdiodev, |
| 660 | int devad, u32 regnum, u16 mask, |
| 661 | u16 set) |
| 662 | { |
| 663 | return mdiobus_c45_modify_changed(bus: mdiodev->bus, addr: mdiodev->addr, devad, |
| 664 | regnum, mask, set); |
| 665 | } |
| 666 | |
| 667 | static inline int mdiodev_c45_read(struct mdio_device *mdiodev, int devad, |
| 668 | u16 regnum) |
| 669 | { |
| 670 | return mdiobus_c45_read(bus: mdiodev->bus, addr: mdiodev->addr, devad, regnum); |
| 671 | } |
| 672 | |
| 673 | static inline int mdiodev_c45_write(struct mdio_device *mdiodev, u32 devad, |
| 674 | u16 regnum, u16 val) |
| 675 | { |
| 676 | return mdiobus_c45_write(bus: mdiodev->bus, addr: mdiodev->addr, devad, regnum, |
| 677 | val); |
| 678 | } |
| 679 | |
| 680 | int mdiobus_register_device(struct mdio_device *mdiodev); |
| 681 | int mdiobus_unregister_device(struct mdio_device *mdiodev); |
| 682 | bool mdiobus_is_registered_device(struct mii_bus *bus, int addr); |
| 683 | struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr); |
| 684 | |
| 685 | /** |
| 686 | * mdio_module_driver() - Helper macro for registering mdio drivers |
| 687 | * @_mdio_driver: driver to register |
| 688 | * |
| 689 | * Helper macro for MDIO drivers which do not do anything special in module |
| 690 | * init/exit. Each module may only use this macro once, and calling it |
| 691 | * replaces module_init() and module_exit(). |
| 692 | */ |
| 693 | #define mdio_module_driver(_mdio_driver) \ |
| 694 | static int __init mdio_module_init(void) \ |
| 695 | { \ |
| 696 | return mdio_driver_register(&_mdio_driver); \ |
| 697 | } \ |
| 698 | module_init(mdio_module_init); \ |
| 699 | static void __exit mdio_module_exit(void) \ |
| 700 | { \ |
| 701 | mdio_driver_unregister(&_mdio_driver); \ |
| 702 | } \ |
| 703 | module_exit(mdio_module_exit) |
| 704 | |
| 705 | #endif /* __LINUX_MDIO_H__ */ |
| 706 | |