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 MAC_SYM_PAUSE = BIT(0), 25 MAC_ASYM_PAUSE = BIT(1), 26 MAC_10HD = BIT(2), 27 MAC_10FD = BIT(3), 28 MAC_10 = MAC_10HD | MAC_10FD, 29 MAC_100HD = BIT(4), 30 MAC_100FD = BIT(5), 31 MAC_100 = MAC_100HD | MAC_100FD, 32 MAC_1000HD = BIT(6), 33 MAC_1000FD = BIT(7), 34 MAC_1000 = MAC_1000HD | MAC_1000FD, 35 MAC_2500FD = BIT(8), 36 MAC_5000FD = BIT(9), 37 MAC_10000FD = BIT(10), 38 MAC_20000FD = BIT(11), 39 MAC_25000FD = BIT(12), 40 MAC_40000FD = BIT(13), 41 MAC_50000FD = BIT(14), 42 MAC_56000FD = BIT(15), 43 MAC_100000FD = BIT(16), 44 MAC_200000FD = BIT(17), 45 MAC_400000FD = BIT(18), 46 }; 47 48 static inline bool phylink_autoneg_inband(unsigned int mode) 49 { 50 return mode == MLO_AN_INBAND; 51 } 52 53 /** 54 * struct phylink_link_state - link state structure 55 * @advertising: ethtool bitmask containing advertised link modes 56 * @lp_advertising: ethtool bitmask containing link partner advertised link 57 * modes 58 * @interface: link &typedef phy_interface_t mode 59 * @speed: link speed, one of the SPEED_* constants. 60 * @duplex: link duplex mode, one of DUPLEX_* constants. 61 * @pause: link pause state, described by MLO_PAUSE_* constants. 62 * @link: true if the link is up. 63 * @an_enabled: true if autonegotiation is enabled/desired. 64 * @an_complete: true if autonegotiation has completed. 65 */ 66 struct phylink_link_state { 67 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 68 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising); 69 phy_interface_t interface; 70 int speed; 71 int duplex; 72 int pause; 73 unsigned int link:1; 74 unsigned int an_enabled:1; 75 unsigned int an_complete:1; 76 }; 77 78 enum phylink_op_type { 79 PHYLINK_NETDEV = 0, 80 PHYLINK_DEV, 81 }; 82 83 /** 84 * struct phylink_config - PHYLINK configuration structure 85 * @dev: a pointer to a struct device associated with the MAC 86 * @type: operation type of PHYLINK instance 87 * @pcs_poll: MAC PCS cannot provide link change interrupt 88 * @poll_fixed_state: if true, starts link_poll, 89 * if MAC link is at %MLO_AN_FIXED mode. 90 * @ovr_an_inband: if true, override PCS to MLO_AN_INBAND 91 * @get_fixed_state: callback to execute to determine the fixed link state, 92 * if MAC link is at %MLO_AN_FIXED mode. 93 * @supported_interfaces: bitmap describing which PHY_INTERFACE_MODE_xxx 94 * are supported by the MAC/PCS. 95 * @mac_capabilities: MAC pause/speed/duplex capabilities. 96 */ 97 struct phylink_config { 98 struct device *dev; 99 enum phylink_op_type type; 100 bool pcs_poll; 101 bool poll_fixed_state; 102 bool ovr_an_inband; 103 void (*get_fixed_state)(struct phylink_config *config, 104 struct phylink_link_state *state); 105 DECLARE_PHY_INTERFACE_MASK(supported_interfaces); 106 unsigned long mac_capabilities; 107 }; 108 109 /** 110 * struct phylink_mac_ops - MAC operations structure. 111 * @validate: Validate and update the link configuration. 112 * @mac_pcs_get_state: Read the current link state from the hardware. 113 * @mac_prepare: prepare for a major reconfiguration of the interface. 114 * @mac_config: configure the MAC for the selected mode and state. 115 * @mac_finish: finish a major reconfiguration of the interface. 116 * @mac_an_restart: restart 802.3z BaseX autonegotiation. 117 * @mac_link_down: take the link down. 118 * @mac_link_up: allow the link to come up. 119 * 120 * The individual methods are described more fully below. 121 */ 122 struct phylink_mac_ops { 123 void (*validate)(struct phylink_config *config, 124 unsigned long *supported, 125 struct phylink_link_state *state); 126 void (*mac_pcs_get_state)(struct phylink_config *config, 127 struct phylink_link_state *state); 128 int (*mac_prepare)(struct phylink_config *config, unsigned int mode, 129 phy_interface_t iface); 130 void (*mac_config)(struct phylink_config *config, unsigned int mode, 131 const struct phylink_link_state *state); 132 int (*mac_finish)(struct phylink_config *config, unsigned int mode, 133 phy_interface_t iface); 134 void (*mac_an_restart)(struct phylink_config *config); 135 void (*mac_link_down)(struct phylink_config *config, unsigned int mode, 136 phy_interface_t interface); 137 void (*mac_link_up)(struct phylink_config *config, 138 struct phy_device *phy, unsigned int mode, 139 phy_interface_t interface, int speed, int duplex, 140 bool tx_pause, bool rx_pause); 141 }; 142 143 #if 0 /* For kernel-doc purposes only. */ 144 /** 145 * validate - Validate and update the link configuration 146 * @config: a pointer to a &struct phylink_config. 147 * @supported: ethtool bitmask for supported link modes. 148 * @state: a pointer to a &struct phylink_link_state. 149 * 150 * Clear bits in the @supported and @state->advertising masks that 151 * are not supportable by the MAC. 152 * 153 * Note that the PHY may be able to transform from one connection 154 * technology to another, so, eg, don't clear 1000BaseX just 155 * because the MAC is unable to BaseX mode. This is more about 156 * clearing unsupported speeds and duplex settings. The port modes 157 * should not be cleared; phylink_set_port_modes() will help with this. 158 * 159 * If the @state->interface mode is %PHY_INTERFACE_MODE_1000BASEX 160 * or %PHY_INTERFACE_MODE_2500BASEX, select the appropriate mode 161 * based on @state->advertising and/or @state->speed and update 162 * @state->interface accordingly. See phylink_helper_basex_speed(). 163 * 164 * When @config->supported_interfaces has been set, phylink will iterate 165 * over the supported interfaces to determine the full capability of the 166 * MAC. The validation function must not print errors if @state->interface 167 * is set to an unexpected value. 168 * 169 * When @config->supported_interfaces is empty, phylink will call this 170 * function with @state->interface set to %PHY_INTERFACE_MODE_NA, and 171 * expects the MAC driver to return all supported link modes. 172 * 173 * If the @state->interface mode is not supported, then the @supported 174 * mask must be cleared. 175 */ 176 void validate(struct phylink_config *config, unsigned long *supported, 177 struct phylink_link_state *state); 178 179 /** 180 * mac_pcs_get_state() - Read the current inband link state from the hardware 181 * @config: a pointer to a &struct phylink_config. 182 * @state: a pointer to a &struct phylink_link_state. 183 * 184 * Read the current inband link state from the MAC PCS, reporting the 185 * current speed in @state->speed, duplex mode in @state->duplex, pause 186 * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits, 187 * negotiation completion state in @state->an_complete, and link up state 188 * in @state->link. If possible, @state->lp_advertising should also be 189 * populated. 190 */ 191 void mac_pcs_get_state(struct phylink_config *config, 192 struct phylink_link_state *state); 193 194 /** 195 * mac_prepare() - prepare to change the PHY interface mode 196 * @config: a pointer to a &struct phylink_config. 197 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND. 198 * @iface: interface mode to switch to 199 * 200 * phylink will call this method at the beginning of a full initialisation 201 * of the link, which includes changing the interface mode or at initial 202 * startup time. It may be called for the current mode. The MAC driver 203 * should perform whatever actions are required, e.g. disabling the 204 * Serdes PHY. 205 * 206 * This will be the first call in the sequence: 207 * - mac_prepare() 208 * - mac_config() 209 * - pcs_config() 210 * - possible pcs_an_restart() 211 * - mac_finish() 212 * 213 * Returns zero on success, or negative errno on failure which will be 214 * reported to the kernel log. 215 */ 216 int mac_prepare(struct phylink_config *config, unsigned int mode, 217 phy_interface_t iface); 218 219 /** 220 * mac_config() - configure the MAC for the selected mode and state 221 * @config: a pointer to a &struct phylink_config. 222 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND. 223 * @state: a pointer to a &struct phylink_link_state. 224 * 225 * Note - not all members of @state are valid. In particular, 226 * @state->lp_advertising, @state->link, @state->an_complete are never 227 * guaranteed to be correct, and so any mac_config() implementation must 228 * never reference these fields. 229 * 230 * (this requires a rewrite - please refer to mac_link_up() for situations 231 * where the PCS and MAC are not tightly integrated.) 232 * 233 * In all negotiation modes, as defined by @mode, @state->pause indicates the 234 * pause settings which should be applied as follows. If %MLO_PAUSE_AN is not 235 * set, %MLO_PAUSE_TX and %MLO_PAUSE_RX indicate whether the MAC should send 236 * pause frames and/or act on received pause frames respectively. Otherwise, 237 * the results of in-band negotiation/status from the MAC PCS should be used 238 * to control the MAC pause mode settings. 239 * 240 * The action performed depends on the currently selected mode: 241 * 242 * %MLO_AN_FIXED, %MLO_AN_PHY: 243 * Configure for non-inband negotiation mode, where the link settings 244 * are completely communicated via mac_link_up(). The physical link 245 * protocol from the MAC is specified by @state->interface. 246 * 247 * @state->advertising may be used, but is not required. 248 * 249 * Older drivers (prior to the mac_link_up() change) may use @state->speed, 250 * @state->duplex and @state->pause to configure the MAC, but this is 251 * deprecated; such drivers should be converted to use mac_link_up(). 252 * 253 * Other members of @state must be ignored. 254 * 255 * Valid state members: interface, advertising. 256 * Deprecated state members: speed, duplex, pause. 257 * 258 * %MLO_AN_INBAND: 259 * place the link in an inband negotiation mode (such as 802.3z 260 * 1000base-X or Cisco SGMII mode depending on the @state->interface 261 * mode). In both cases, link state management (whether the link 262 * is up or not) is performed by the MAC, and reported via the 263 * mac_pcs_get_state() callback. Changes in link state must be made 264 * by calling phylink_mac_change(). 265 * 266 * Interface mode specific details are mentioned below. 267 * 268 * If in 802.3z mode, the link speed is fixed, dependent on the 269 * @state->interface. Duplex and pause modes are negotiated via 270 * the in-band configuration word. Advertised pause modes are set 271 * according to the @state->an_enabled and @state->advertising 272 * flags. Beware of MACs which only support full duplex at gigabit 273 * and higher speeds. 274 * 275 * If in Cisco SGMII mode, the link speed and duplex mode are passed 276 * in the serial bitstream 16-bit configuration word, and the MAC 277 * should be configured to read these bits and acknowledge the 278 * configuration word. Nothing is advertised by the MAC. The MAC is 279 * responsible for reading the configuration word and configuring 280 * itself accordingly. 281 * 282 * Valid state members: interface, an_enabled, pause, advertising. 283 * 284 * Implementations are expected to update the MAC to reflect the 285 * requested settings - i.o.w., if nothing has changed between two 286 * calls, no action is expected. If only flow control settings have 287 * changed, flow control should be updated *without* taking the link 288 * down. This "update" behaviour is critical to avoid bouncing the 289 * link up status. 290 */ 291 void mac_config(struct phylink_config *config, unsigned int mode, 292 const struct phylink_link_state *state); 293 294 /** 295 * mac_finish() - finish a to change the PHY interface mode 296 * @config: a pointer to a &struct phylink_config. 297 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND. 298 * @iface: interface mode to switch to 299 * 300 * phylink will call this if it called mac_prepare() to allow the MAC to 301 * complete any necessary steps after the MAC and PCS have been configured 302 * for the @mode and @iface. E.g. a MAC driver may wish to re-enable the 303 * Serdes PHY here if it was previously disabled by mac_prepare(). 304 * 305 * Returns zero on success, or negative errno on failure which will be 306 * reported to the kernel log. 307 */ 308 int mac_finish(struct phylink_config *config, unsigned int mode, 309 phy_interface_t iface); 310 311 /** 312 * mac_an_restart() - restart 802.3z BaseX autonegotiation 313 * @config: a pointer to a &struct phylink_config. 314 */ 315 void mac_an_restart(struct phylink_config *config); 316 317 /** 318 * mac_link_down() - take the link down 319 * @config: a pointer to a &struct phylink_config. 320 * @mode: link autonegotiation mode 321 * @interface: link &typedef phy_interface_t mode 322 * 323 * If @mode is not an in-band negotiation mode (as defined by 324 * phylink_autoneg_inband()), force the link down and disable any 325 * Energy Efficient Ethernet MAC configuration. Interface type 326 * selection must be done in mac_config(). 327 */ 328 void mac_link_down(struct phylink_config *config, unsigned int mode, 329 phy_interface_t interface); 330 331 /** 332 * mac_link_up() - allow the link to come up 333 * @config: a pointer to a &struct phylink_config. 334 * @phy: any attached phy 335 * @mode: link autonegotiation mode 336 * @interface: link &typedef phy_interface_t mode 337 * @speed: link speed 338 * @duplex: link duplex 339 * @tx_pause: link transmit pause enablement status 340 * @rx_pause: link receive pause enablement status 341 * 342 * Configure the MAC for an established link. 343 * 344 * @speed, @duplex, @tx_pause and @rx_pause indicate the finalised link 345 * settings, and should be used to configure the MAC block appropriately 346 * where these settings are not automatically conveyed from the PCS block, 347 * or if in-band negotiation (as defined by phylink_autoneg_inband(@mode)) 348 * is disabled. 349 * 350 * Note that when 802.3z in-band negotiation is in use, it is possible 351 * that the user wishes to override the pause settings, and this should 352 * be allowed when considering the implementation of this method. 353 * 354 * If in-band negotiation mode is disabled, allow the link to come up. If 355 * @phy is non-%NULL, configure Energy Efficient Ethernet by calling 356 * phy_init_eee() and perform appropriate MAC configuration for EEE. 357 * Interface type selection must be done in mac_config(). 358 */ 359 void mac_link_up(struct phylink_config *config, struct phy_device *phy, 360 unsigned int mode, phy_interface_t interface, 361 int speed, int duplex, bool tx_pause, bool rx_pause); 362 #endif 363 364 struct phylink_pcs_ops; 365 366 /** 367 * struct phylink_pcs - PHYLINK PCS instance 368 * @ops: a pointer to the &struct phylink_pcs_ops structure 369 * @poll: poll the PCS for link changes 370 * 371 * This structure is designed to be embedded within the PCS private data, 372 * and will be passed between phylink and the PCS. 373 */ 374 struct phylink_pcs { 375 const struct phylink_pcs_ops *ops; 376 bool poll; 377 }; 378 379 /** 380 * struct phylink_pcs_ops - MAC PCS operations structure. 381 * @pcs_get_state: read the current MAC PCS link state from the hardware. 382 * @pcs_config: configure the MAC PCS for the selected mode and state. 383 * @pcs_an_restart: restart 802.3z BaseX autonegotiation. 384 * @pcs_link_up: program the PCS for the resolved link configuration 385 * (where necessary). 386 */ 387 struct phylink_pcs_ops { 388 void (*pcs_get_state)(struct phylink_pcs *pcs, 389 struct phylink_link_state *state); 390 int (*pcs_config)(struct phylink_pcs *pcs, unsigned int mode, 391 phy_interface_t interface, 392 const unsigned long *advertising, 393 bool permit_pause_to_mac); 394 void (*pcs_an_restart)(struct phylink_pcs *pcs); 395 void (*pcs_link_up)(struct phylink_pcs *pcs, unsigned int mode, 396 phy_interface_t interface, int speed, int duplex); 397 }; 398 399 #if 0 /* For kernel-doc purposes only. */ 400 /** 401 * pcs_get_state() - Read the current inband link state from the hardware 402 * @pcs: a pointer to a &struct phylink_pcs. 403 * @state: a pointer to a &struct phylink_link_state. 404 * 405 * Read the current inband link state from the MAC PCS, reporting the 406 * current speed in @state->speed, duplex mode in @state->duplex, pause 407 * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits, 408 * negotiation completion state in @state->an_complete, and link up state 409 * in @state->link. If possible, @state->lp_advertising should also be 410 * populated. 411 * 412 * When present, this overrides mac_pcs_get_state() in &struct 413 * phylink_mac_ops. 414 */ 415 void pcs_get_state(struct phylink_pcs *pcs, 416 struct phylink_link_state *state); 417 418 /** 419 * pcs_config() - Configure the PCS mode and advertisement 420 * @pcs: a pointer to a &struct phylink_pcs. 421 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND. 422 * @interface: interface mode to be used 423 * @advertising: adertisement ethtool link mode mask 424 * @permit_pause_to_mac: permit forwarding pause resolution to MAC 425 * 426 * Configure the PCS for the operating mode, the interface mode, and set 427 * the advertisement mask. @permit_pause_to_mac indicates whether the 428 * hardware may forward the pause mode resolution to the MAC. 429 * 430 * When operating in %MLO_AN_INBAND, inband should always be enabled, 431 * otherwise inband should be disabled. 432 * 433 * For SGMII, there is no advertisement from the MAC side, the PCS should 434 * be programmed to acknowledge the inband word from the PHY. 435 * 436 * For 1000BASE-X, the advertisement should be programmed into the PCS. 437 * 438 * For most 10GBASE-R, there is no advertisement. 439 */ 440 int pcs_config(struct phylink_pcs *pcs, unsigned int mode, 441 phy_interface_t interface, const unsigned long *advertising, 442 bool permit_pause_to_mac); 443 444 /** 445 * pcs_an_restart() - restart 802.3z BaseX autonegotiation 446 * @pcs: a pointer to a &struct phylink_pcs. 447 * 448 * When PCS ops are present, this overrides mac_an_restart() in &struct 449 * phylink_mac_ops. 450 */ 451 void pcs_an_restart(struct phylink_pcs *pcs); 452 453 /** 454 * pcs_link_up() - program the PCS for the resolved link configuration 455 * @pcs: a pointer to a &struct phylink_pcs. 456 * @mode: link autonegotiation mode 457 * @interface: link &typedef phy_interface_t mode 458 * @speed: link speed 459 * @duplex: link duplex 460 * 461 * This call will be made just before mac_link_up() to inform the PCS of 462 * the resolved link parameters. For example, a PCS operating in SGMII 463 * mode without in-band AN needs to be manually configured for the link 464 * and duplex setting. Otherwise, this should be a no-op. 465 */ 466 void pcs_link_up(struct phylink_pcs *pcs, unsigned int mode, 467 phy_interface_t interface, int speed, int duplex); 468 #endif 469 470 void phylink_get_linkmodes(unsigned long *linkmodes, phy_interface_t interface, 471 unsigned long mac_capabilities); 472 void phylink_generic_validate(struct phylink_config *config, 473 unsigned long *supported, 474 struct phylink_link_state *state); 475 476 struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *, 477 phy_interface_t iface, 478 const struct phylink_mac_ops *mac_ops); 479 void phylink_set_pcs(struct phylink *, struct phylink_pcs *pcs); 480 void phylink_destroy(struct phylink *); 481 482 int phylink_connect_phy(struct phylink *, struct phy_device *); 483 int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags); 484 int phylink_fwnode_phy_connect(struct phylink *pl, 485 struct fwnode_handle *fwnode, 486 u32 flags); 487 void phylink_disconnect_phy(struct phylink *); 488 489 void phylink_mac_change(struct phylink *, bool up); 490 491 void phylink_start(struct phylink *); 492 void phylink_stop(struct phylink *); 493 494 void phylink_suspend(struct phylink *pl, bool mac_wol); 495 void phylink_resume(struct phylink *pl); 496 497 void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *); 498 int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *); 499 500 int phylink_ethtool_ksettings_get(struct phylink *, 501 struct ethtool_link_ksettings *); 502 int phylink_ethtool_ksettings_set(struct phylink *, 503 const struct ethtool_link_ksettings *); 504 int phylink_ethtool_nway_reset(struct phylink *); 505 void phylink_ethtool_get_pauseparam(struct phylink *, 506 struct ethtool_pauseparam *); 507 int phylink_ethtool_set_pauseparam(struct phylink *, 508 struct ethtool_pauseparam *); 509 int phylink_get_eee_err(struct phylink *); 510 int phylink_init_eee(struct phylink *, bool); 511 int phylink_ethtool_get_eee(struct phylink *, struct ethtool_eee *); 512 int phylink_ethtool_set_eee(struct phylink *, struct ethtool_eee *); 513 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int); 514 int phylink_speed_down(struct phylink *pl, bool sync); 515 int phylink_speed_up(struct phylink *pl); 516 517 #define phylink_zero(bm) \ 518 bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS) 519 #define __phylink_do_bit(op, bm, mode) \ 520 op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm) 521 522 #define phylink_set(bm, mode) __phylink_do_bit(__set_bit, bm, mode) 523 #define phylink_clear(bm, mode) __phylink_do_bit(__clear_bit, bm, mode) 524 #define phylink_test(bm, mode) __phylink_do_bit(test_bit, bm, mode) 525 526 void phylink_set_port_modes(unsigned long *bits); 527 void phylink_set_10g_modes(unsigned long *mask); 528 void phylink_helper_basex_speed(struct phylink_link_state *state); 529 530 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state, 531 u16 bmsr, u16 lpa); 532 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs, 533 struct phylink_link_state *state); 534 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface, 535 const unsigned long *advertising); 536 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode, 537 phy_interface_t interface, 538 const unsigned long *advertising); 539 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs); 540 541 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs, 542 struct phylink_link_state *state); 543 544 void phylink_decode_usxgmii_word(struct phylink_link_state *state, 545 uint16_t lpa); 546 #endif 547