Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/mii.h: definitions for MII-compatible transceivers |
| 4 | * Originally drivers/net/sunhme.h. |
| 5 | * |
| 6 | * Copyright (C) 1996, 1999, 2001 David S. Miller (davem@redhat.com) |
| 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #ifndef __LINUX_MII_H__ |
| 9 | #define __LINUX_MII_H__ |
| 10 | |
David Woodhouse | c3ce7e2 | 2006-04-29 01:53:47 +0100 | [diff] [blame] | 11 | |
| 12 | #include <linux/if.h> |
Andrew Lunn | b31cdff | 2018-09-29 23:04:09 +0200 | [diff] [blame] | 13 | #include <linux/linkmode.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 14 | #include <uapi/linux/mii.h> |
David Woodhouse | c3ce7e2 | 2006-04-29 01:53:47 +0100 | [diff] [blame] | 15 | |
| 16 | struct ethtool_cmd; |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | struct mii_if_info { |
| 19 | int phy_id; |
| 20 | int advertising; |
| 21 | int phy_id_mask; |
| 22 | int reg_num_mask; |
| 23 | |
| 24 | unsigned int full_duplex : 1; /* is full duplex? */ |
| 25 | unsigned int force_media : 1; /* is autoneg. disabled? */ |
| 26 | unsigned int supports_gmii : 1; /* are GMII registers supported? */ |
| 27 | |
| 28 | struct net_device *dev; |
| 29 | int (*mdio_read) (struct net_device *dev, int phy_id, int location); |
| 30 | void (*mdio_write) (struct net_device *dev, int phy_id, int location, int val); |
| 31 | }; |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | extern int mii_link_ok (struct mii_if_info *mii); |
| 34 | extern int mii_nway_restart (struct mii_if_info *mii); |
| 35 | extern int mii_ethtool_gset(struct mii_if_info *mii, struct ethtool_cmd *ecmd); |
yuval.shaia@oracle.com | 82c01a8 | 2017-06-04 20:22:00 +0300 | [diff] [blame] | 36 | extern void mii_ethtool_get_link_ksettings( |
Philippe Reynes | bc8ee59 | 2016-11-01 16:32:25 +0100 | [diff] [blame] | 37 | struct mii_if_info *mii, struct ethtool_link_ksettings *cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | extern int mii_ethtool_sset(struct mii_if_info *mii, struct ethtool_cmd *ecmd); |
Philippe Reynes | bc8ee59 | 2016-11-01 16:32:25 +0100 | [diff] [blame] | 39 | extern int mii_ethtool_set_link_ksettings( |
| 40 | struct mii_if_info *mii, const struct ethtool_link_ksettings *cmd); |
Dale Farnsworth | 43ec6e9 | 2005-08-23 10:30:29 -0700 | [diff] [blame] | 41 | extern int mii_check_gmii_support(struct mii_if_info *mii); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | extern void mii_check_link (struct mii_if_info *mii); |
| 43 | extern unsigned int mii_check_media (struct mii_if_info *mii, |
| 44 | unsigned int ok_to_print, |
| 45 | unsigned int init_media); |
| 46 | extern int generic_mii_ioctl(struct mii_if_info *mii_if, |
Mark Einon | c44f7eb | 2011-08-31 23:22:16 +0000 | [diff] [blame] | 47 | struct mii_ioctl_data *mii_data, int cmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | unsigned int *duplex_changed); |
| 49 | |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static inline struct mii_ioctl_data *if_mii(struct ifreq *rq) |
| 52 | { |
| 53 | return (struct mii_ioctl_data *) &rq->ifr_ifru; |
| 54 | } |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | /** |
| 57 | * mii_nway_result |
| 58 | * @negotiated: value of MII ANAR and'd with ANLPAR |
| 59 | * |
| 60 | * Given a set of MII abilities, check each bit and returns the |
| 61 | * currently supported media, in the priority order defined by |
| 62 | * IEEE 802.3u. We use LPA_xxx constants but note this is not the |
| 63 | * value of LPA solely, as described above. |
| 64 | * |
| 65 | * The one exception to IEEE 802.3u is that 100baseT4 is placed |
| 66 | * between 100T-full and 100T-half. If your phy does not support |
| 67 | * 100T4 this is fine. If your phy places 100T4 elsewhere in the |
| 68 | * priority order, you will need to roll your own function. |
| 69 | */ |
| 70 | static inline unsigned int mii_nway_result (unsigned int negotiated) |
| 71 | { |
| 72 | unsigned int ret; |
| 73 | |
| 74 | if (negotiated & LPA_100FULL) |
| 75 | ret = LPA_100FULL; |
| 76 | else if (negotiated & LPA_100BASE4) |
| 77 | ret = LPA_100BASE4; |
| 78 | else if (negotiated & LPA_100HALF) |
| 79 | ret = LPA_100HALF; |
| 80 | else if (negotiated & LPA_10FULL) |
| 81 | ret = LPA_10FULL; |
| 82 | else |
| 83 | ret = LPA_10HALF; |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * mii_duplex |
| 90 | * @duplex_lock: Non-zero if duplex is locked at full |
| 91 | * @negotiated: value of MII ANAR and'd with ANLPAR |
| 92 | * |
| 93 | * A small helper function for a common case. Returns one |
| 94 | * if the media is operating or locked at full duplex, and |
| 95 | * returns zero otherwise. |
| 96 | */ |
| 97 | static inline unsigned int mii_duplex (unsigned int duplex_lock, |
| 98 | unsigned int negotiated) |
| 99 | { |
| 100 | if (duplex_lock) |
| 101 | return 1; |
| 102 | if (mii_nway_result(negotiated) & LPA_DUPLEX) |
| 103 | return 1; |
| 104 | return 0; |
| 105 | } |
| 106 | |
Steve Glendinning | bc02ff9 | 2008-12-16 02:00:48 -0800 | [diff] [blame] | 107 | /** |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 108 | * ethtool_adv_to_mii_adv_t |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 109 | * @ethadv: the ethtool advertisement settings |
| 110 | * |
| 111 | * A small helper function that translates ethtool advertisement |
| 112 | * settings to phy autonegotiation advertisements for the |
| 113 | * MII_ADVERTISE register. |
| 114 | */ |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 115 | static inline u32 ethtool_adv_to_mii_adv_t(u32 ethadv) |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 116 | { |
| 117 | u32 result = 0; |
| 118 | |
| 119 | if (ethadv & ADVERTISED_10baseT_Half) |
| 120 | result |= ADVERTISE_10HALF; |
| 121 | if (ethadv & ADVERTISED_10baseT_Full) |
| 122 | result |= ADVERTISE_10FULL; |
| 123 | if (ethadv & ADVERTISED_100baseT_Half) |
| 124 | result |= ADVERTISE_100HALF; |
| 125 | if (ethadv & ADVERTISED_100baseT_Full) |
| 126 | result |= ADVERTISE_100FULL; |
| 127 | if (ethadv & ADVERTISED_Pause) |
| 128 | result |= ADVERTISE_PAUSE_CAP; |
| 129 | if (ethadv & ADVERTISED_Asym_Pause) |
| 130 | result |= ADVERTISE_PAUSE_ASYM; |
| 131 | |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | /** |
Andrew Lunn | f954a04 | 2018-09-29 23:04:14 +0200 | [diff] [blame] | 136 | * linkmode_adv_to_mii_adv_t |
| 137 | * @advertising: the linkmode advertisement settings |
| 138 | * |
| 139 | * A small helper function that translates linkmode advertisement |
| 140 | * settings to phy autonegotiation advertisements for the |
| 141 | * MII_ADVERTISE register. |
| 142 | */ |
| 143 | static inline u32 linkmode_adv_to_mii_adv_t(unsigned long *advertising) |
| 144 | { |
| 145 | u32 result = 0; |
| 146 | |
| 147 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, advertising)) |
| 148 | result |= ADVERTISE_10HALF; |
| 149 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, advertising)) |
| 150 | result |= ADVERTISE_10FULL; |
| 151 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, advertising)) |
| 152 | result |= ADVERTISE_100HALF; |
| 153 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, advertising)) |
| 154 | result |= ADVERTISE_100FULL; |
| 155 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising)) |
| 156 | result |= ADVERTISE_PAUSE_CAP; |
| 157 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising)) |
| 158 | result |= ADVERTISE_PAUSE_ASYM; |
| 159 | |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | /** |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 164 | * mii_adv_to_ethtool_adv_t |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 165 | * @adv: value of the MII_ADVERTISE register |
| 166 | * |
| 167 | * A small helper function that translates MII_ADVERTISE bits |
| 168 | * to ethtool advertisement settings. |
| 169 | */ |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 170 | static inline u32 mii_adv_to_ethtool_adv_t(u32 adv) |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 171 | { |
| 172 | u32 result = 0; |
| 173 | |
| 174 | if (adv & ADVERTISE_10HALF) |
| 175 | result |= ADVERTISED_10baseT_Half; |
| 176 | if (adv & ADVERTISE_10FULL) |
| 177 | result |= ADVERTISED_10baseT_Full; |
| 178 | if (adv & ADVERTISE_100HALF) |
| 179 | result |= ADVERTISED_100baseT_Half; |
| 180 | if (adv & ADVERTISE_100FULL) |
| 181 | result |= ADVERTISED_100baseT_Full; |
| 182 | if (adv & ADVERTISE_PAUSE_CAP) |
| 183 | result |= ADVERTISED_Pause; |
| 184 | if (adv & ADVERTISE_PAUSE_ASYM) |
| 185 | result |= ADVERTISED_Asym_Pause; |
| 186 | |
| 187 | return result; |
| 188 | } |
| 189 | |
| 190 | /** |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 191 | * ethtool_adv_to_mii_ctrl1000_t |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 192 | * @ethadv: the ethtool advertisement settings |
| 193 | * |
| 194 | * A small helper function that translates ethtool advertisement |
| 195 | * settings to phy autonegotiation advertisements for the |
| 196 | * MII_CTRL1000 register when in 1000T mode. |
| 197 | */ |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 198 | static inline u32 ethtool_adv_to_mii_ctrl1000_t(u32 ethadv) |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 199 | { |
| 200 | u32 result = 0; |
| 201 | |
| 202 | if (ethadv & ADVERTISED_1000baseT_Half) |
| 203 | result |= ADVERTISE_1000HALF; |
| 204 | if (ethadv & ADVERTISED_1000baseT_Full) |
| 205 | result |= ADVERTISE_1000FULL; |
| 206 | |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | /** |
Andrew Lunn | f954a04 | 2018-09-29 23:04:14 +0200 | [diff] [blame] | 211 | * linkmode_adv_to_mii_ctrl1000_t |
Andrew Lunn | fe19191 | 2018-11-10 23:43:35 +0100 | [diff] [blame] | 212 | * @advertising: the linkmode advertisement settings |
Andrew Lunn | f954a04 | 2018-09-29 23:04:14 +0200 | [diff] [blame] | 213 | * |
| 214 | * A small helper function that translates linkmode advertisement |
| 215 | * settings to phy autonegotiation advertisements for the |
| 216 | * MII_CTRL1000 register when in 1000T mode. |
| 217 | */ |
| 218 | static inline u32 linkmode_adv_to_mii_ctrl1000_t(unsigned long *advertising) |
| 219 | { |
| 220 | u32 result = 0; |
| 221 | |
| 222 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 223 | advertising)) |
| 224 | result |= ADVERTISE_1000HALF; |
| 225 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 226 | advertising)) |
| 227 | result |= ADVERTISE_1000FULL; |
| 228 | |
| 229 | return result; |
| 230 | } |
| 231 | |
| 232 | /** |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 233 | * mii_ctrl1000_to_ethtool_adv_t |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 234 | * @adv: value of the MII_CTRL1000 register |
| 235 | * |
| 236 | * A small helper function that translates MII_CTRL1000 |
| 237 | * bits, when in 1000Base-T mode, to ethtool |
| 238 | * advertisement settings. |
| 239 | */ |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 240 | static inline u32 mii_ctrl1000_to_ethtool_adv_t(u32 adv) |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 241 | { |
| 242 | u32 result = 0; |
| 243 | |
| 244 | if (adv & ADVERTISE_1000HALF) |
| 245 | result |= ADVERTISED_1000baseT_Half; |
| 246 | if (adv & ADVERTISE_1000FULL) |
| 247 | result |= ADVERTISED_1000baseT_Full; |
| 248 | |
| 249 | return result; |
| 250 | } |
| 251 | |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 252 | /** |
| 253 | * mii_lpa_to_ethtool_lpa_t |
| 254 | * @adv: value of the MII_LPA register |
| 255 | * |
| 256 | * A small helper function that translates MII_LPA |
| 257 | * bits, when in 1000Base-T mode, to ethtool |
| 258 | * LP advertisement settings. |
| 259 | */ |
| 260 | static inline u32 mii_lpa_to_ethtool_lpa_t(u32 lpa) |
| 261 | { |
| 262 | u32 result = 0; |
| 263 | |
| 264 | if (lpa & LPA_LPACK) |
| 265 | result |= ADVERTISED_Autoneg; |
| 266 | |
| 267 | return result | mii_adv_to_ethtool_adv_t(lpa); |
| 268 | } |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 269 | |
| 270 | /** |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 271 | * mii_stat1000_to_ethtool_lpa_t |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 272 | * @adv: value of the MII_STAT1000 register |
| 273 | * |
| 274 | * A small helper function that translates MII_STAT1000 |
| 275 | * bits, when in 1000Base-T mode, to ethtool |
| 276 | * advertisement settings. |
| 277 | */ |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 278 | static inline u32 mii_stat1000_to_ethtool_lpa_t(u32 lpa) |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 279 | { |
| 280 | u32 result = 0; |
| 281 | |
| 282 | if (lpa & LPA_1000HALF) |
| 283 | result |= ADVERTISED_1000baseT_Half; |
| 284 | if (lpa & LPA_1000FULL) |
| 285 | result |= ADVERTISED_1000baseT_Full; |
| 286 | |
| 287 | return result; |
| 288 | } |
| 289 | |
| 290 | /** |
Andrew Lunn | 78a24df | 2018-12-05 21:49:41 +0100 | [diff] [blame] | 291 | * mii_stat1000_mod_linkmode_lpa_t |
Andrew Lunn | c0ec3c2 | 2018-11-10 23:43:34 +0100 | [diff] [blame] | 292 | * @advertising: target the linkmode advertisement settings |
| 293 | * @adv: value of the MII_STAT1000 register |
| 294 | * |
| 295 | * A small helper function that translates MII_STAT1000 bits, when in |
Andrew Lunn | 78a24df | 2018-12-05 21:49:41 +0100 | [diff] [blame] | 296 | * 1000Base-T mode, to linkmode advertisement settings. Other bits in |
| 297 | * advertising are not changes. |
Andrew Lunn | c0ec3c2 | 2018-11-10 23:43:34 +0100 | [diff] [blame] | 298 | */ |
Andrew Lunn | 78a24df | 2018-12-05 21:49:41 +0100 | [diff] [blame] | 299 | static inline void mii_stat1000_mod_linkmode_lpa_t(unsigned long *advertising, |
| 300 | u32 lpa) |
Andrew Lunn | c0ec3c2 | 2018-11-10 23:43:34 +0100 | [diff] [blame] | 301 | { |
Andrew Lunn | 78a24df | 2018-12-05 21:49:41 +0100 | [diff] [blame] | 302 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 303 | advertising, lpa & LPA_1000HALF); |
| 304 | |
| 305 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 306 | advertising, lpa & LPA_1000FULL); |
Andrew Lunn | c0ec3c2 | 2018-11-10 23:43:34 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 310 | * ethtool_adv_to_mii_adv_x |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 311 | * @ethadv: the ethtool advertisement settings |
| 312 | * |
| 313 | * A small helper function that translates ethtool advertisement |
| 314 | * settings to phy autonegotiation advertisements for the |
| 315 | * MII_CTRL1000 register when in 1000Base-X mode. |
| 316 | */ |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 317 | static inline u32 ethtool_adv_to_mii_adv_x(u32 ethadv) |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 318 | { |
| 319 | u32 result = 0; |
| 320 | |
| 321 | if (ethadv & ADVERTISED_1000baseT_Half) |
| 322 | result |= ADVERTISE_1000XHALF; |
| 323 | if (ethadv & ADVERTISED_1000baseT_Full) |
| 324 | result |= ADVERTISE_1000XFULL; |
| 325 | if (ethadv & ADVERTISED_Pause) |
| 326 | result |= ADVERTISE_1000XPAUSE; |
| 327 | if (ethadv & ADVERTISED_Asym_Pause) |
| 328 | result |= ADVERTISE_1000XPSE_ASYM; |
| 329 | |
| 330 | return result; |
| 331 | } |
| 332 | |
| 333 | /** |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 334 | * mii_adv_to_ethtool_adv_x |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 335 | * @adv: value of the MII_CTRL1000 register |
| 336 | * |
| 337 | * A small helper function that translates MII_CTRL1000 |
| 338 | * bits, when in 1000Base-X mode, to ethtool |
| 339 | * advertisement settings. |
| 340 | */ |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 341 | static inline u32 mii_adv_to_ethtool_adv_x(u32 adv) |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 342 | { |
| 343 | u32 result = 0; |
| 344 | |
| 345 | if (adv & ADVERTISE_1000XHALF) |
| 346 | result |= ADVERTISED_1000baseT_Half; |
| 347 | if (adv & ADVERTISE_1000XFULL) |
| 348 | result |= ADVERTISED_1000baseT_Full; |
| 349 | if (adv & ADVERTISE_1000XPAUSE) |
| 350 | result |= ADVERTISED_Pause; |
| 351 | if (adv & ADVERTISE_1000XPSE_ASYM) |
| 352 | result |= ADVERTISED_Asym_Pause; |
| 353 | |
| 354 | return result; |
| 355 | } |
| 356 | |
| 357 | /** |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 358 | * mii_lpa_to_ethtool_lpa_x |
| 359 | * @adv: value of the MII_LPA register |
| 360 | * |
| 361 | * A small helper function that translates MII_LPA |
| 362 | * bits, when in 1000Base-X mode, to ethtool |
| 363 | * LP advertisement settings. |
| 364 | */ |
| 365 | static inline u32 mii_lpa_to_ethtool_lpa_x(u32 lpa) |
| 366 | { |
| 367 | u32 result = 0; |
| 368 | |
| 369 | if (lpa & LPA_LPACK) |
| 370 | result |= ADVERTISED_Autoneg; |
| 371 | |
| 372 | return result | mii_adv_to_ethtool_adv_x(lpa); |
| 373 | } |
| 374 | |
| 375 | /** |
Andrew Lunn | d335193 | 2018-12-05 21:49:43 +0100 | [diff] [blame] | 376 | * mii_adv_mod_linkmode_adv_t |
| 377 | * @advertising:pointer to destination link mode. |
| 378 | * @adv: value of the MII_ADVERTISE register |
| 379 | * |
| 380 | * A small helper function that translates MII_ADVERTISE bits to |
| 381 | * linkmode advertisement settings. Leaves other bits unchanged. |
| 382 | */ |
| 383 | static inline void mii_adv_mod_linkmode_adv_t(unsigned long *advertising, |
| 384 | u32 adv) |
| 385 | { |
| 386 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, |
| 387 | advertising, adv & ADVERTISE_10HALF); |
| 388 | |
| 389 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, |
| 390 | advertising, adv & ADVERTISE_10FULL); |
| 391 | |
| 392 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, |
| 393 | advertising, adv & ADVERTISE_100HALF); |
| 394 | |
| 395 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 396 | advertising, adv & ADVERTISE_100FULL); |
| 397 | |
| 398 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising, |
| 399 | adv & ADVERTISE_PAUSE_CAP); |
| 400 | |
| 401 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 402 | advertising, adv & ADVERTISE_PAUSE_ASYM); |
| 403 | } |
| 404 | |
| 405 | /** |
Andrew Lunn | edc7ccb | 2018-09-29 23:04:12 +0200 | [diff] [blame] | 406 | * mii_adv_to_linkmode_adv_t |
| 407 | * @advertising:pointer to destination link mode. |
| 408 | * @adv: value of the MII_ADVERTISE register |
| 409 | * |
| 410 | * A small helper function that translates MII_ADVERTISE bits |
Andrew Lunn | 5f15eed | 2018-12-05 21:49:40 +0100 | [diff] [blame] | 411 | * to linkmode advertisement settings. Clears the old value |
| 412 | * of advertising. |
Andrew Lunn | edc7ccb | 2018-09-29 23:04:12 +0200 | [diff] [blame] | 413 | */ |
| 414 | static inline void mii_adv_to_linkmode_adv_t(unsigned long *advertising, |
| 415 | u32 adv) |
| 416 | { |
| 417 | linkmode_zero(advertising); |
| 418 | |
Andrew Lunn | d335193 | 2018-12-05 21:49:43 +0100 | [diff] [blame] | 419 | mii_adv_mod_linkmode_adv_t(advertising, adv); |
Andrew Lunn | edc7ccb | 2018-09-29 23:04:12 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
Andrew Lunn | c0ec3c2 | 2018-11-10 23:43:34 +0100 | [diff] [blame] | 423 | * mii_lpa_to_linkmode_lpa_t |
| 424 | * @adv: value of the MII_LPA register |
| 425 | * |
| 426 | * A small helper function that translates MII_LPA bits, when in |
Andrew Lunn | 5f15eed | 2018-12-05 21:49:40 +0100 | [diff] [blame] | 427 | * 1000Base-T mode, to linkmode LP advertisement settings. Clears the |
| 428 | * old value of advertising |
Andrew Lunn | c0ec3c2 | 2018-11-10 23:43:34 +0100 | [diff] [blame] | 429 | */ |
| 430 | static inline void mii_lpa_to_linkmode_lpa_t(unsigned long *lp_advertising, |
| 431 | u32 lpa) |
| 432 | { |
Andrew Lunn | 5f15eed | 2018-12-05 21:49:40 +0100 | [diff] [blame] | 433 | mii_adv_to_linkmode_adv_t(lp_advertising, lpa); |
| 434 | |
Andrew Lunn | c0ec3c2 | 2018-11-10 23:43:34 +0100 | [diff] [blame] | 435 | if (lpa & LPA_LPACK) |
| 436 | linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, |
| 437 | lp_advertising); |
| 438 | |
Andrew Lunn | c0ec3c2 | 2018-11-10 23:43:34 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /** |
Andrew Lunn | d335193 | 2018-12-05 21:49:43 +0100 | [diff] [blame] | 442 | * mii_lpa_mod_linkmode_lpa_t |
| 443 | * @adv: value of the MII_LPA register |
| 444 | * |
| 445 | * A small helper function that translates MII_LPA bits, when in |
| 446 | * 1000Base-T mode, to linkmode LP advertisement settings. Leaves |
| 447 | * other bits unchanged. |
| 448 | */ |
| 449 | static inline void mii_lpa_mod_linkmode_lpa_t(unsigned long *lp_advertising, |
| 450 | u32 lpa) |
| 451 | { |
| 452 | mii_adv_mod_linkmode_adv_t(lp_advertising, lpa); |
| 453 | |
Andrew Lunn | 6dbd009 | 2018-12-05 21:49:44 +0100 | [diff] [blame] | 454 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, |
| 455 | lp_advertising, lpa & LPA_LPACK); |
Andrew Lunn | d335193 | 2018-12-05 21:49:43 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 459 | * linkmode_adv_to_lcl_adv_t |
| 460 | * @advertising:pointer to linkmode advertising |
Andrew Lunn | 5f991f7 | 2018-09-29 23:04:13 +0200 | [diff] [blame] | 461 | * |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 462 | * A small helper function that translates linkmode advertising to LVL |
Andrew Lunn | 5f991f7 | 2018-09-29 23:04:13 +0200 | [diff] [blame] | 463 | * pause capabilities. |
| 464 | */ |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 465 | static inline u32 linkmode_adv_to_lcl_adv_t(unsigned long *advertising) |
Andrew Lunn | 5f991f7 | 2018-09-29 23:04:13 +0200 | [diff] [blame] | 466 | { |
| 467 | u32 lcl_adv = 0; |
| 468 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 469 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 470 | advertising)) |
Andrew Lunn | 5f991f7 | 2018-09-29 23:04:13 +0200 | [diff] [blame] | 471 | lcl_adv |= ADVERTISE_PAUSE_CAP; |
Claudiu Manoil | 7f07e5f | 2019-03-26 11:48:57 +0200 | [diff] [blame] | 472 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 473 | advertising)) |
Andrew Lunn | 5f991f7 | 2018-09-29 23:04:13 +0200 | [diff] [blame] | 474 | lcl_adv |= ADVERTISE_PAUSE_ASYM; |
| 475 | |
| 476 | return lcl_adv; |
| 477 | } |
| 478 | |
| 479 | /** |
Ben Hutchings | a8c3083 | 2009-04-29 08:19:03 +0000 | [diff] [blame] | 480 | * mii_advertise_flowctrl - get flow control advertisement flags |
| 481 | * @cap: Flow control capabilities (FLOW_CTRL_RX, FLOW_CTRL_TX or both) |
| 482 | */ |
| 483 | static inline u16 mii_advertise_flowctrl(int cap) |
| 484 | { |
| 485 | u16 adv = 0; |
| 486 | |
| 487 | if (cap & FLOW_CTRL_RX) |
| 488 | adv = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; |
| 489 | if (cap & FLOW_CTRL_TX) |
| 490 | adv ^= ADVERTISE_PAUSE_ASYM; |
| 491 | |
| 492 | return adv; |
| 493 | } |
| 494 | |
| 495 | /** |
Steve Glendinning | bc02ff9 | 2008-12-16 02:00:48 -0800 | [diff] [blame] | 496 | * mii_resolve_flowctrl_fdx |
| 497 | * @lcladv: value of MII ADVERTISE register |
| 498 | * @rmtadv: value of MII LPA register |
| 499 | * |
| 500 | * Resolve full duplex flow control as per IEEE 802.3-2005 table 28B-3 |
| 501 | */ |
| 502 | static inline u8 mii_resolve_flowctrl_fdx(u16 lcladv, u16 rmtadv) |
| 503 | { |
| 504 | u8 cap = 0; |
| 505 | |
Ben Hutchings | 44c22ee | 2009-04-29 08:15:05 +0000 | [diff] [blame] | 506 | if (lcladv & rmtadv & ADVERTISE_PAUSE_CAP) { |
| 507 | cap = FLOW_CTRL_TX | FLOW_CTRL_RX; |
| 508 | } else if (lcladv & rmtadv & ADVERTISE_PAUSE_ASYM) { |
| 509 | if (lcladv & ADVERTISE_PAUSE_CAP) |
| 510 | cap = FLOW_CTRL_RX; |
| 511 | else if (rmtadv & ADVERTISE_PAUSE_CAP) |
Steve Glendinning | bc02ff9 | 2008-12-16 02:00:48 -0800 | [diff] [blame] | 512 | cap = FLOW_CTRL_TX; |
| 513 | } |
| 514 | |
| 515 | return cap; |
| 516 | } |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | #endif /* __LINUX_MII_H__ */ |