1 #ifndef NETDEV_PCS_H 2 #define NETDEV_PCS_H 3 4 #include <linux/phy.h> 5 #include <linux/spinlock.h> 6 #include <linux/workqueue.h> 7 8 struct device_node; 9 struct ethtool_cmd; 10 struct fwnode_handle; 11 struct net_device; 12 13 enum { 14 MLO_PAUSE_NONE, 15 MLO_PAUSE_RX = BIT(0), 16 MLO_PAUSE_TX = BIT(1), 17 MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX, 18 MLO_PAUSE_AN = BIT(2), 19 20 MLO_AN_PHY = 0, /* Conventional PHY */ 21 MLO_AN_FIXED, /* Fixed-link mode */ 22 MLO_AN_INBAND, /* In-band protocol */ 23 }; 24 25 static inline bool phylink_autoneg_inband(unsigned int mode) 26 { 27 return mode == MLO_AN_INBAND; 28 } 29 30 /** 31 * struct phylink_link_state - link state structure 32 * @advertising: ethtool bitmask containing advertised link modes 33 * @lp_advertising: ethtool bitmask containing link partner advertised link 34 * modes 35 * @interface: link &typedef phy_interface_t mode 36 * @speed: link speed, one of the SPEED_* constants. 37 * @duplex: link duplex mode, one of DUPLEX_* constants. 38 * @pause: link pause state, described by MLO_PAUSE_* constants. 39 * @link: true if the link is up. 40 * @an_enabled: true if autonegotiation is enabled/desired. 41 * @an_complete: true if autonegotiation has completed. 42 */ 43 struct phylink_link_state { 44 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 45 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising); 46 phy_interface_t interface; 47 int speed; 48 int duplex; 49 int pause; 50 unsigned int link:1; 51 unsigned int an_enabled:1; 52 unsigned int an_complete:1; 53 }; 54 55 enum phylink_op_type { 56 PHYLINK_NETDEV = 0, 57 PHYLINK_DEV, 58 }; 59 60 /** 61 * struct phylink_config - PHYLINK configuration structure 62 * @dev: a pointer to a struct device associated with the MAC 63 * @type: operation type of PHYLINK instance 64 * @pcs_poll: MAC PCS cannot provide link change interrupt 65 * @poll_fixed_state: if true, starts link_poll, 66 * if MAC link is at %MLO_AN_FIXED mode. 67 * @get_fixed_state: callback to execute to determine the fixed link state, 68 * if MAC link is at %MLO_AN_FIXED mode. 69 */ 70 struct phylink_config { 71 struct device *dev; 72 enum phylink_op_type type; 73 bool pcs_poll; 74 bool poll_fixed_state; 75 void (*get_fixed_state)(struct phylink_config *config, 76 struct phylink_link_state *state); 77 }; 78 79 /** 80 * struct phylink_mac_ops - MAC operations structure. 81 * @validate: Validate and update the link configuration. 82 * @mac_pcs_get_state: Read the current link state from the hardware. 83 * @mac_config: configure the MAC for the selected mode and state. 84 * @mac_an_restart: restart 802.3z BaseX autonegotiation. 85 * @mac_link_down: take the link down. 86 * @mac_link_up: allow the link to come up. 87 * 88 * The individual methods are described more fully below. 89 */ 90 struct phylink_mac_ops { 91 void (*validate)(struct phylink_config *config, 92 unsigned long *supported, 93 struct phylink_link_state *state); 94 void (*mac_pcs_get_state)(struct phylink_config *config, 95 struct phylink_link_state *state); 96 void (*mac_config)(struct phylink_config *config, unsigned int mode, 97 const struct phylink_link_state *state); 98 void (*mac_an_restart)(struct phylink_config *config); 99 void (*mac_link_down)(struct phylink_config *config, unsigned int mode, 100 phy_interface_t interface); 101 void (*mac_link_up)(struct phylink_config *config, 102 struct phy_device *phy, unsigned int mode, 103 phy_interface_t interface, int speed, int duplex, 104 bool tx_pause, bool rx_pause); 105 }; 106 107 #if 0 /* For kernel-doc purposes only. */ 108 /** 109 * validate - Validate and update the link configuration 110 * @config: a pointer to a &struct phylink_config. 111 * @supported: ethtool bitmask for supported link modes. 112 * @state: a pointer to a &struct phylink_link_state. 113 * 114 * Clear bits in the @supported and @state->advertising masks that 115 * are not supportable by the MAC. 116 * 117 * Note that the PHY may be able to transform from one connection 118 * technology to another, so, eg, don't clear 1000BaseX just 119 * because the MAC is unable to BaseX mode. This is more about 120 * clearing unsupported speeds and duplex settings. The port modes 121 * should not be cleared; phylink_set_port_modes() will help with this. 122 * 123 * If the @state->interface mode is %PHY_INTERFACE_MODE_1000BASEX 124 * or %PHY_INTERFACE_MODE_2500BASEX, select the appropriate mode 125 * based on @state->advertising and/or @state->speed and update 126 * @state->interface accordingly. See phylink_helper_basex_speed(). 127 * 128 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink expects the 129 * MAC driver to return all supported link modes. 130 * 131 * If the @state->interface mode is not supported, then the @supported 132 * mask must be cleared. 133 */ 134 void validate(struct phylink_config *config, unsigned long *supported, 135 struct phylink_link_state *state); 136 137 /** 138 * mac_pcs_get_state() - Read the current inband link state from the hardware 139 * @config: a pointer to a &struct phylink_config. 140 * @state: a pointer to a &struct phylink_link_state. 141 * 142 * Read the current inband link state from the MAC PCS, reporting the 143 * current speed in @state->speed, duplex mode in @state->duplex, pause 144 * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits, 145 * negotiation completion state in @state->an_complete, and link up state 146 * in @state->link. If possible, @state->lp_advertising should also be 147 * populated. 148 */ 149 void mac_pcs_get_state(struct phylink_config *config, 150 struct phylink_link_state *state); 151 152 /** 153 * mac_config() - configure the MAC for the selected mode and state 154 * @config: a pointer to a &struct phylink_config. 155 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND. 156 * @state: a pointer to a &struct phylink_link_state. 157 * 158 * Note - not all members of @state are valid. In particular, 159 * @state->lp_advertising, @state->link, @state->an_complete are never 160 * guaranteed to be correct, and so any mac_config() implementation must 161 * never reference these fields. 162 * 163 * (this requires a rewrite - please refer to mac_link_up() for situations 164 * where the PCS and MAC are not tightly integrated.) 165 * 166 * In all negotiation modes, as defined by @mode, @state->pause indicates the 167 * pause settings which should be applied as follows. If %MLO_PAUSE_AN is not 168 * set, %MLO_PAUSE_TX and %MLO_PAUSE_RX indicate whether the MAC should send 169 * pause frames and/or act on received pause frames respectively. Otherwise, 170 * the results of in-band negotiation/status from the MAC PCS should be used 171 * to control the MAC pause mode settings. 172 * 173 * The action performed depends on the currently selected mode: 174 * 175 * %MLO_AN_FIXED, %MLO_AN_PHY: 176 * Configure for non-inband negotiation mode, where the link settings 177 * are completely communicated via mac_link_up(). The physical link 178 * protocol from the MAC is specified by @state->interface. 179 * 180 * @state->advertising may be used, but is not required. 181 * 182 * Older drivers (prior to the mac_link_up() change) may use @state->speed, 183 * @state->duplex and @state->pause to configure the MAC, but this is 184 * deprecated; such drivers should be converted to use mac_link_up(). 185 * 186 * Other members of @state must be ignored. 187 * 188 * Valid state members: interface, advertising. 189 * Deprecated state members: speed, duplex, pause. 190 * 191 * %MLO_AN_INBAND: 192 * place the link in an inband negotiation mode (such as 802.3z 193 * 1000base-X or Cisco SGMII mode depending on the @state->interface 194 * mode). In both cases, link state management (whether the link 195 * is up or not) is performed by the MAC, and reported via the 196 * mac_pcs_get_state() callback. Changes in link state must be made 197 * by calling phylink_mac_change(). 198 * 199 * Interface mode specific details are mentioned below. 200 * 201 * If in 802.3z mode, the link speed is fixed, dependent on the 202 * @state->interface. Duplex and pause modes are negotiated via 203 * the in-band configuration word. Advertised pause modes are set 204 * according to the @state->an_enabled and @state->advertising 205 * flags. Beware of MACs which only support full duplex at gigabit 206 * and higher speeds. 207 * 208 * If in Cisco SGMII mode, the link speed and duplex mode are passed 209 * in the serial bitstream 16-bit configuration word, and the MAC 210 * should be configured to read these bits and acknowledge the 211 * configuration word. Nothing is advertised by the MAC. The MAC is 212 * responsible for reading the configuration word and configuring 213 * itself accordingly. 214 * 215 * Valid state members: interface, an_enabled, pause, advertising. 216 * 217 * Implementations are expected to update the MAC to reflect the 218 * requested settings - i.o.w., if nothing has changed between two 219 * calls, no action is expected. If only flow control settings have 220 * changed, flow control should be updated *without* taking the link 221 * down. This "update" behaviour is critical to avoid bouncing the 222 * link up status. 223 */ 224 void mac_config(struct phylink_config *config, unsigned int mode, 225 const struct phylink_link_state *state); 226 227 /** 228 * mac_an_restart() - restart 802.3z BaseX autonegotiation 229 * @config: a pointer to a &struct phylink_config. 230 */ 231 void mac_an_restart(struct phylink_config *config); 232 233 /** 234 * mac_link_down() - take the link down 235 * @config: a pointer to a &struct phylink_config. 236 * @mode: link autonegotiation mode 237 * @interface: link &typedef phy_interface_t mode 238 * 239 * If @mode is not an in-band negotiation mode (as defined by 240 * phylink_autoneg_inband()), force the link down and disable any 241 * Energy Efficient Ethernet MAC configuration. Interface type 242 * selection must be done in mac_config(). 243 */ 244 void mac_link_down(struct phylink_config *config, unsigned int mode, 245 phy_interface_t interface); 246 247 /** 248 * mac_link_up() - allow the link to come up 249 * @config: a pointer to a &struct phylink_config. 250 * @phy: any attached phy 251 * @mode: link autonegotiation mode 252 * @interface: link &typedef phy_interface_t mode 253 * @speed: link speed 254 * @duplex: link duplex 255 * @tx_pause: link transmit pause enablement status 256 * @rx_pause: link receive pause enablement status 257 * 258 * Configure the MAC for an established link. 259 * 260 * @speed, @duplex, @tx_pause and @rx_pause indicate the finalised link 261 * settings, and should be used to configure the MAC block appropriately 262 * where these settings are not automatically conveyed from the PCS block, 263 * or if in-band negotiation (as defined by phylink_autoneg_inband(@mode)) 264 * is disabled. 265 * 266 * Note that when 802.3z in-band negotiation is in use, it is possible 267 * that the user wishes to override the pause settings, and this should 268 * be allowed when considering the implementation of this method. 269 * 270 * If in-band negotiation mode is disabled, allow the link to come up. If 271 * @phy is non-%NULL, configure Energy Efficient Ethernet by calling 272 * phy_init_eee() and perform appropriate MAC configuration for EEE. 273 * Interface type selection must be done in mac_config(). 274 */ 275 void mac_link_up(struct phylink_config *config, struct phy_device *phy, 276 unsigned int mode, phy_interface_t interface, 277 int speed, int duplex, bool tx_pause, bool rx_pause); 278 #endif 279 280 /** 281 * struct phylink_pcs_ops - MAC PCS operations structure. 282 * @pcs_get_state: read the current MAC PCS link state from the hardware. 283 * @pcs_config: configure the MAC PCS for the selected mode and state. 284 * @pcs_an_restart: restart 802.3z BaseX autonegotiation. 285 * @pcs_link_up: program the PCS for the resolved link configuration 286 * (where necessary). 287 */ 288 struct phylink_pcs_ops { 289 void (*pcs_get_state)(struct phylink_config *config, 290 struct phylink_link_state *state); 291 int (*pcs_config)(struct phylink_config *config, unsigned int mode, 292 phy_interface_t interface, 293 const unsigned long *advertising); 294 void (*pcs_an_restart)(struct phylink_config *config); 295 void (*pcs_link_up)(struct phylink_config *config, unsigned int mode, 296 phy_interface_t interface, int speed, int duplex); 297 }; 298 299 #if 0 /* For kernel-doc purposes only. */ 300 /** 301 * pcs_get_state() - Read the current inband link state from the hardware 302 * @config: a pointer to a &struct phylink_config. 303 * @state: a pointer to a &struct phylink_link_state. 304 * 305 * Read the current inband link state from the MAC PCS, reporting the 306 * current speed in @state->speed, duplex mode in @state->duplex, pause 307 * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits, 308 * negotiation completion state in @state->an_complete, and link up state 309 * in @state->link. If possible, @state->lp_advertising should also be 310 * populated. 311 * 312 * When present, this overrides mac_pcs_get_state() in &struct 313 * phylink_mac_ops. 314 */ 315 void pcs_get_state(struct phylink_config *config, 316 struct phylink_link_state *state); 317 318 /** 319 * pcs_config() - Configure the PCS mode and advertisement 320 * @config: a pointer to a &struct phylink_config. 321 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND. 322 * @interface: interface mode to be used 323 * @advertising: adertisement ethtool link mode mask 324 * 325 * Configure the PCS for the operating mode, the interface mode, and set 326 * the advertisement mask. 327 * 328 * When operating in %MLO_AN_INBAND, inband should always be enabled, 329 * otherwise inband should be disabled. 330 * 331 * For SGMII, there is no advertisement from the MAC side, the PCS should 332 * be programmed to acknowledge the inband word from the PHY. 333 * 334 * For 1000BASE-X, the advertisement should be programmed into the PCS. 335 * 336 * For most 10GBASE-R, there is no advertisement. 337 */ 338 int (*pcs_config)(struct phylink_config *config, unsigned int mode, 339 phy_interface_t interface, const unsigned long *advertising); 340 341 /** 342 * pcs_an_restart() - restart 802.3z BaseX autonegotiation 343 * @config: a pointer to a &struct phylink_config. 344 * 345 * When PCS ops are present, this overrides mac_an_restart() in &struct 346 * phylink_mac_ops. 347 */ 348 void (*pcs_an_restart)(struct phylink_config *config); 349 350 /** 351 * pcs_link_up() - program the PCS for the resolved link configuration 352 * @config: a pointer to a &struct phylink_config. 353 * @mode: link autonegotiation mode 354 * @interface: link &typedef phy_interface_t mode 355 * @speed: link speed 356 * @duplex: link duplex 357 * 358 * This call will be made just before mac_link_up() to inform the PCS of 359 * the resolved link parameters. For example, a PCS operating in SGMII 360 * mode without in-band AN needs to be manually configured for the link 361 * and duplex setting. Otherwise, this should be a no-op. 362 */ 363 void (*pcs_link_up)(struct phylink_config *config, unsigned int mode, 364 phy_interface_t interface, int speed, int duplex); 365 #endif 366 367 struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *, 368 phy_interface_t iface, 369 const struct phylink_mac_ops *mac_ops); 370 void phylink_add_pcs(struct phylink *, const struct phylink_pcs_ops *ops); 371 void phylink_destroy(struct phylink *); 372 373 int phylink_connect_phy(struct phylink *, struct phy_device *); 374 int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags); 375 void phylink_disconnect_phy(struct phylink *); 376 377 void phylink_mac_change(struct phylink *, bool up); 378 379 void phylink_start(struct phylink *); 380 void phylink_stop(struct phylink *); 381 382 void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *); 383 int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *); 384 385 int phylink_ethtool_ksettings_get(struct phylink *, 386 struct ethtool_link_ksettings *); 387 int phylink_ethtool_ksettings_set(struct phylink *, 388 const struct ethtool_link_ksettings *); 389 int phylink_ethtool_nway_reset(struct phylink *); 390 void phylink_ethtool_get_pauseparam(struct phylink *, 391 struct ethtool_pauseparam *); 392 int phylink_ethtool_set_pauseparam(struct phylink *, 393 struct ethtool_pauseparam *); 394 int phylink_get_eee_err(struct phylink *); 395 int phylink_init_eee(struct phylink *, bool); 396 int phylink_ethtool_get_eee(struct phylink *, struct ethtool_eee *); 397 int phylink_ethtool_set_eee(struct phylink *, struct ethtool_eee *); 398 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int); 399 400 #define phylink_zero(bm) \ 401 bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS) 402 #define __phylink_do_bit(op, bm, mode) \ 403 op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm) 404 405 #define phylink_set(bm, mode) __phylink_do_bit(__set_bit, bm, mode) 406 #define phylink_clear(bm, mode) __phylink_do_bit(__clear_bit, bm, mode) 407 #define phylink_test(bm, mode) __phylink_do_bit(test_bit, bm, mode) 408 409 void phylink_set_port_modes(unsigned long *bits); 410 void phylink_helper_basex_speed(struct phylink_link_state *state); 411 412 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs, 413 struct phylink_link_state *state); 414 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs, 415 phy_interface_t interface, 416 const unsigned long *advertising); 417 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs); 418 419 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs, 420 struct phylink_link_state *state); 421 #endif 422